Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Generic factory

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

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

Summary

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 {
public:
    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。

原型的例子:

Examples

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

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

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

  NS_IMPL_ISUPPORTS

  NS_IMETHOD DoSomething();
};

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

为这个类创建一个工厂:

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

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

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

Background

(This is based on my original news posting <[email protected]>.)

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:

通过以下类可以简化我们调用类工厂的实现。即COM的实现方式。

// 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 {
 public:
    typedef nsresult (*CreatorProcPtr) (nsISupports *aOuter,
                                        REFNSIID aIID, void **aResult);
 
    nsGenericFactory(CreatorProcPtr creator);
    virtual ~nsGenericFactory();
 
    NS_DECL_ISUPPORTS
 
    NS_IMETHOD CreateInstance(nsISupports *aOuter, REFNSIID aIID, void **aResult);
 
    NS_IMETHOD LockFactory(PRBool aLock);
 
 private:
    CreatorProcPtr mCreator;
 };
 
 nsGenericFactory::nsGenericFactory(CreatorProcPtr creator)
    :  mCreator(creator)
 {
    NS_INIT_REFCNT();
 }
 
 nsGenericFactory::~nsGenericFactory() {}
 
 static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
 
 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);
   factory->AddRef();
   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 mozilla.org contributors; content available under a Creative Commons license | Details.

文档标签和贡献者

 此页面的贡献者: ziyunfei, hanyuxinting
 最后编辑者: ziyunfei,