Design Pattern ->Abstract Factory

            Layering & Contract Philosophy With additional indirection

Abstract Factory

 1 //The example code is as following:
 2 class CAbstractProductA;
 3 class CAbstractProductB;
 4 // multiple families of products.
 5 class CProductA1:public CAbstractProductA;
 6 class CProductB1:public CAbstractProductB;
 7 class CProductA2:public CAbstractProductA;
 8 class CProductB2:public CAbstractProductB;
 9 
10 class CAbstractFactory
11 {
12 public: 
13     virtual CAbstractProductA* CreateProductA() = 0;
14     virtual CAbstractProductA* CreateProductB() = 0;
15 };
16 class CConreateFactory1:public CAbstractFactory;  //indirection layer
17 {
18 public:
19     virtual CAbstractProductA* CreateProductA() { return new CProductA1; }
20     virtual CAbstractProductB* CreateProductB() { return new CProductB1; }
21 }
22 class CConreateFactory2:public CAbstractFactory;//indirection layer
23 {
24 public:
25     virtual CAbstractProductA* CreateProductA() { return new CProductA2; }
26     virtual CAbstractProductB* CreateProductB() { return new CProductB2; }
27 }
28 
29 class CClient
30 {
31 public:
32     CAbstractFactory  *pFactory = NULL;
33     void main()
34     {
35            if ( the system is windows )
{
36 pFactory = new CConreateFactory1();
}
37 else
{
38 pFactory = new CConreateFactory2(); 39 } 40 CAbstractProductA *pProductA = pFactory->CreateProductA(); 41 CAbstractProductB *pProductB = pFactory->CreateProductB(); 42 } 43 };

Applicability
Use the Abstract Factory pattern when:

  • A system should be independent of how its products are created, compose /decompose, and represented.
  • A system should be configured with one of multiple families of products.
  • A family of related product objects is designed to be used together, and you need to enforce this constraint.
  • You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations. Interface-Oriented programming, Open For Extension, Close For Change,These All is said to Users or Client. Users don’t care or never know what is changed, so called “Close For Change”.
  • You want to create a set of product without changing client code. Just create a new pointer to the object instantiated with class derived from base class AbstractClass.

Participants

  •  AbstractFactory (WidgetFactory): declares an interface for operations that create abstract product objects.
  • ConcreteFactory (MotifWidgetFactory, PMWidgetFactory):implements the operations to create concrete product objects.
  • AbstractProduct (Window, ScrollBar):declares an interface for a type of product object.
  • ConcreteProduct (MotifWindow, MotifScrollBar): concrete / specific defines a product object to be created by the corresponding concrete factory,implements the AbstractProduct interface.
  • Client: uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Collaborations

  • Normally a single instance of a ConcreteFactory class is created at run-time.This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.The Client get the a pointer or reference from the Singleton object created with ConcreteFactory at run-time.
  • AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

From:Design Patterns:Elements of Reusable Object-Oriented Software, by GoF

原文地址:https://www.cnblogs.com/iiiDragon/p/3216347.html