Generic factory

In XPCOM, a generic factory is a factory created using the facilities in xpcom/glue/nsIGenericFactory.h.

XPCOM中,泛型工厂定义在这里 xpcom/glue/nsIGenericFactory.h


Most XPCOM factories can be very simple. Rick Potts wrote a templated-based generic factory (nsFactory<T>) that simplifies the factory creation process that just requires writing a CreateInstance() method. The new nsIGenericFactory interface takes this a step further, by providing a single interface that can be reused anytime a simple implementation of nsIFactory is needed. Here is the interface, and a description of its use.

大多数XPCOM工厂都很简单。Rick Potts编写的基于模板的泛型工厂(nsFactory<T>),简化了工厂的创建过程,只需要调用CreateInstance()方法即可。新的 nsIGenericFactory 接口更进一步,提供一个单件接口,可复用 nsIFactory的实现。

 * Provides a Generic nsIFactory implementation that can be used by
 * DLLs with very simple factory needs.
class nsIGenericFactory : public nsIFactory {
    static const nsIID& IID() { static nsIID iid = NS_IGENERICFACTORY_IID; return iid; }

    typedef NS_CALLBACK(ConstructorProcPtr) (nsISupports *aOuter, REFNSIID aIID, void **aResult);

     * Establishes the generic factory's constructor function, which will be called
     * by CreateInstance.
    NS_IMETHOD SetConstructor(ConstructorProcPtr constructor) = 0;

Using nsIGenericFactory is simple. Create a new instance from the repository with a CID of NS_GENERICFACTORY_CID, and and IID of NS_IGENERICFACTORY_IID. Define a constructor function that matches the ConstructorProcPtr prototype, and call nsIGenericFactory::SetConstructor with a pointer to that function. You're done. You now have a fully functional factory object.

从 nsIGenericFactory 库创建实例需要 NS_GENERICFACTORY_CID 和 NS_IGENERICFACTORY_IID。创建一个构造函数,要与ConstructorProcPtr 原型匹配,并且通过指针调用方法 nsIGenericFactory::SetConstructor。



class nsIComponent : public nsISupports {
  NS_IMETHOD DoSomething() = 0;

class MyComponent : public nsIComponent {
  virtual ~MyComponent();

  static NS_METHOD Create(nsISupports *aOuter, REFNSIID aIID, void **aResult);


  NS_IMETHOD DoSomething();

To create a factory for this class, simply write the following:


nsIFactory* NewComponentFactory(nsIRepository* repository)
    nsIGenericFactory* factory = NULL;
    nsresult res = repository->CreateInstance(kGenericFactoryCID, NULL, nsIGenericFactory::IID(), &factory);
    if (res == NS_OK) {
    return factory;

This example assumes that the XPCOM repository is available as an interface (which it soon will be).

本例中假设 XPCOM库可作为一个接口使用。


(This is based on my original news posting <>.)

We seem to be creating a huge number of different factory implementations. It seems to me that we can cut down on code size (all those QueryInterface, AddRef, Release implementations) if we just use the following class for all of the simple factories:


// Idea:  Why not create a generic factory facility so we can avoid
 // duplication of so much nsIFactory code? All we need is an allocator
 // function, the rest of the implementation is exactly the same.
 #include "nsIFactory.h"
 class nsGenericFactory : public nsIFactory {
    typedef nsresult (*CreatorProcPtr) (nsISupports *aOuter,
                                        REFNSIID aIID, void **aResult);
    nsGenericFactory(CreatorProcPtr creator);
    virtual ~nsGenericFactory();
    NS_IMETHOD CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult);
    NS_IMETHOD LockFactory(PRBool aLock);
    CreatorProcPtr mCreator;
 nsGenericFactory::nsGenericFactory(CreatorProcPtr creator)
    :  mCreator(creator)
 nsGenericFactory::~nsGenericFactory() {}
 NS_IMPL_ISUPPORTS(nsGenericFactory, kIFactoryIID)
 NS_IMETHODIMP nsGenericFactory::CreateInstance(nsISupports *aOuter,
                                                REFNSIID aIID, void **aResult)
    return mCreator(aOuter, aIID, aResult);
 NS_IMETHODIMP nsGenericFactory::LockFactory(PRBool aLock)
    return NS_OK;

Many of our classes already have a static entry point that serves as the creator function, so in most cases, creating a new factory for a class is just:


nsIFactory* NewMallocFactory()
   nsIFactory* factory = new nsGenericFactory(&nsMalloc::Create);
   return factory;

Talking to Warren, he suggests that we even provide a shorthand for this, we should be able to register a factory with just a function pointer.

Original Document Information

  • Author: Patrick Beard
  • Last Updated Date: February 26, 1999
  • Copyright Information: Portions of this content are © 1998–2007 by individual contributors; content available under a Creative Commons license | Details.