zoukankan      html  css  js  c++  java
  • C++模式学习------工厂模式

    工厂模式属于创建型模式,大致可以分为简单工厂模式、抽象工厂模式。

    简单工厂模式,它的主要特点是需要在工厂类中做判断,从而创造相应的产品。

     1 enum PTYPE
     2 {
     3     ProdA = 0,
     4     ProdB = 1
     5 };
     6 
     7 class ProductBase
     8 {
     9 public:
    10     virtual void Show() = 0;
    11 };
    12 
    13 //产品A
    14 class ProductA: public ProductBase
    15 {
    16 public:
    17     ProductA(){show();}
    18     void Show() { cout<<"Product A"<<endl; }
    19 };
    20 
    21 //产品B
    22 class ProductB: public ProductBase
    23 {
    24 public:
    25     ProductB(){show();}
    26     void Show() { cout<<"Product B"<<endl; }
    27 };
    28 
    29 //工厂
    30 class Factory
    31 {
    32 public:
    33     ProductBase* CreateProduct(enum PTYPE type)
    34     {
    35         if(type == ProdA)
    36             return new ProductA(); //生产A
    37         else if(type == ProdB)
    38             return new ProductB(); //生产B
    39         else
    40             return nullptr;
    41     }
    42 };

     抽象工厂模式: 为一组相关的产品定义实例化,提供一系列的接口而无需去定义一些工厂类

     1 enum PTYPE
     2 {
     3     ProdA = 0,
     4     ProdB = 1
     5 };
     6 
     7 class ProductBase
     8 {
     9 public:
    10     virtual void Show() = 0;
    11 };
    12 
    13 //产品A
    14 class ProductA: public ProductBase
    15 {
    16 public:
    17     ProductA(){show();}
    18     void Show() { cout<<"Product A"<<endl; }
    19 };
    20 
    21 //产品B
    22 class ProductB: public ProductBase
    23 {
    24 public:
    25     ProductB(){show();}
    26     void Show() { cout<<"Product B"<<endl; }
    27 };
    28 
    29 class ProductBase_PLUS
    30 {
    31 public:
    32     virtual void Show() = 0;
    33 };
    34 
    35 //产品A-plus
    36 class ProductA_PLUS: public ProductBase_PLUS
    37 {
    38 public:
    39     ProductA_PLUS(){show();}
    40     void Show() { cout<<"Product A-PLUS"<<endl; }
    41 };
    42 
    43 //产品B-plus
    44 class ProductB_PLUS: public ProductBase_PLUS
    45 {
    46 public:
    47     ProductB_PLUS(){show();}
    48     void Show() { cout<<"Product B-PLUS"<<endl; }
    49 };
    50 
    51 class FactoryBase
    52 {
    53 public:
    54     virtual ProductBase* CreateProduct() = 0;
    55     virtual ProductBase_PLUS* CreatePLUSProduct() = 0;
    56 };
    57 
    58 //工厂A
    59 class FactoryA : public FactoryBase
    60 {
    61 public:
    62     ProductBase* CreateProduct()//生产A
    63     {
    64         return new ProductA();
    65     }
    66 
    67     ProductBase_PLUS* CreatePLUSProduct()//生产A-plus
    68     {
    69         return new ProductA_PLUS();
    70     }
    71 };
    72 
    73 //工厂B
    74 class FactoryB : public FactoryBase
    75 {
    76 public:
    77     ProductBase* CreateProduct()//生产B
    78     {
    79         return new ProductB();
    80     }
    81 
    82     ProductBase_PLUS* CreatePLUSProduct()//生产B-plus
    83     {
    84         return new ProductB_PLUS();
    85     }
    86 };
  • 相关阅读:
    ElasticSearch搜索
    Muddery框架
    https和http的区别
    multiprocessing模块创建进程
    进程理论基础
    单例模式
    原型模式
    建造者模式
    StrutsTypeConverter的使用
    UML用例图之泛化(generalization)、扩展(extend)和包含(include)关系--UML一波流系列讲解
  • 原文地址:https://www.cnblogs.com/tyche116/p/8578553.html
Copyright © 2011-2022 走看看