zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    java jdk1.8 32/64位 官方绿色版下载附安装教程
    坡度常用的表示方法
    就此道别
    阿里巴巴矢量图标库(iconfont)批量全选的方法
    thinkphp6.0 集成Alipay 手机和电脑端支付的方法
    法定的属于我的第23个年头已经结束,在今天迎来第24年的第一天。
    世界地图展开图,来自 Simon's World Map
    thinkphp6.0 composer 安装 web-token/jwt-framework 常见出错原因分析及解决方法
    thinkphp6 常用方法文档
    Python获取列表中的最后一个或者倒数第几个的方案
  • 原文地址:https://www.cnblogs.com/iiiDragon/p/3216347.html
Copyright © 2011-2022 走看看