zoukankan      html  css  js  c++  java
  • C++设计模式之-工厂模式的总结

    工厂模式分为3种,即简单工厂模式、工厂方法模式、抽象工厂模式,其实大同小异,总结下来就是:

    简单工厂模式:一个工厂,多个产品。产品需要有一个虚基类。通过传入参数,生成具体产品对象,并利用基类指针指向此对象。通过工厂获取此虚基类指针,通过运行时多态,调用子类实现。

     1 // Factory.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include <iostream>
     6 #include <string>
     7 using namespace std;
     8 class Product
     9 {
    10 public:
    11     virtual void show() = 0;
    12 };
    13 class Product_A:public Product
    14 {
    15 public:
    16     void show() override
    17     {
    18         cout<<"Product_A"<<endl;
    19     }
    20 };
    21 class Product_B:public Product
    22 {
    23 public:
    24     void show() override
    25     {
    26         cout<<"Product_B"<<endl;
    27     }
    28 };
    29 class Factory
    30 {
    31 public:
    32     Product* create(int i)
    33     {
    34         switch(i)
    35         {
    36         case 1:
    37             return new Product_A;
    38             break;
    39         case 2:
    40             return new Product_B;
    41             break;
    42         default:
    43             break;
    44         }
    45     }
    46 };
    47 
    48 int _tmain(int argc, _TCHAR* argv[])
    49 {
    50     Factory *factory = new Factory();
    51     factory->create(1)->show();
    52     factory->create(2)->show();
    53     system("pause");
    54     return 0;
    55 }

     工厂方法模式:多个工厂,多个产品,每个产品对应于一个工厂。此时工厂和产品都是通过虚基类的方式构建。对于简单工厂模式,当要增加一个新产品时候,就需要在工厂类中修改代码,具体表现为多加一个参数,来识别新的产品类型。此时违反了对扩展开放,对修改关闭的原则。基于此,工厂方法模式应运而生。当增加一个新产品时,同时增加一个新工厂。增加新工厂属于扩展,不会修改以前工厂类和产品类的任何代码。可以看过多个独立的简单工厂模式构成了工厂方法模式。

     1 // FactoryM.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include<iostream>
     6 #include <string>
     7 using namespace std;
     8 
     9 class Product
    10 {
    11 public:
    12     virtual void show() = 0;
    13 };
    14 class Product_A:public Product
    15 {
    16 public:
    17      void show() override
    18      {
    19          cout<<"Product_A"<<endl;
    20      }
    21  };
    22 class Product_B:public Product
    23 {
    24 public:
    25      void show() override
    26      {
    27          cout<<"Product_B"<<endl;
    28      }
    29 };
    30  
    31 class Factory
    32 {
    33 public:
    34     virtual Product* create()=0;
    35 };
    36 
    37 class Factory_A:public Factory
    38 {
    39 public:
    40     Product* create()
    41     {
    42         return new Product_A;
    43     }
    44 };
    45 class Factory_B:public Factory
    46 {
    47 public:
    48     Product* create()
    49     {
    50         return new Product_B;
    51     }
    52 };
    53 
    54 int _tmain(int argc, _TCHAR* argv[])
    55 {
    56     Factory_A *product_a = new Factory_A();
    57     Factory_B *product_b = new Factory_B();
    58     product_a->create()->show();
    59     product_b->create()->show();
    60     system("pause");
    61     return 0;
    62 }

     抽象工厂模式:多个工厂,多个产品,并且每个产品可以包含多个型号。此时工厂和产品都是通过虚基类的方式构建。每一个工厂类可以生产同一个产品的多个型号。

     1 // FactoryMUL.cpp : 定义控制台应用程序的入口点。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include <iostream>
     6 #include<string>
     7 using namespace std;
     8 
     9 class Product1
    10 {
    11 public:
    12     virtual void show() = 0;
    13 };
    14 class Product_A1:public Product1
    15 {
    16 public:
    17     void show() override
    18     {
    19         cout<<"Product_A1"<<endl;
    20     }
    21 };
    22 class Product_B1:public Product1
    23 {
    24 public:
    25     void show() override
    26     {
    27         cout<<"Product_B1"<<endl;
    28     }
    29 };
    30 class Product2
    31 {
    32 public:
    33     virtual void show() = 0;
    34 };
    35 class Product_A2:public Product2
    36 {
    37 public:
    38     void show() override
    39     {
    40         cout<<"Product_A2"<<endl;
    41     }
    42 };
    43 class Product_B2:public Product2
    44 {
    45 public:
    46     void show() override
    47     {
    48         cout<<"Product_B2"<<endl;
    49     }
    50 };
    51 class Factory
    52 {
    53 public:
    54     virtual Product1 *create1() = 0;
    55     virtual Product2 *create2() = 0;
    56 };
    57 class Factory_A
    58 {
    59 public:
    60     Product1 *create1(){ return new Product_A1;}
    61     Product2 *create2(){ return new Product_A2;}
    62 };
    63 class Factory_B
    64 {
    65 public:
    66     Product1 *create1(){ return new Product_B1;}
    67     Product2 *create2(){ return new Product_B2;}
    68 };
    69 int _tmain(int argc, _TCHAR* argv[])
    70 {
    71     Factory_A *factoryA = new Factory_A();
    72     factoryA->create1()->show();
    73     factoryA->create2()->show();
    74 
    75     Factory_B *factoryB = new Factory_B();
    76     factoryB->create1()->show();
    77     factoryB->create2()->show();
    78     system("pause");
    79     return 0;
    80 }
  • 相关阅读:
    硬币游戏 Project Euler 232
    屏幕空间的近似全局光照明(Approximative Global Illumination in Screen Space)
    四维之美
    vertex texture fetching in HLSL, and heightfield normal calculation
    一个VS小插件(跳出括号)
    我的算法书籍收藏
    Algorithms.算法概论.习题答案
    UML用例图教程详解
    大连理工大学软件学院博客地址
    快递查询API,我推荐“爱快递”
  • 原文地址:https://www.cnblogs.com/wxmwanggood/p/9273173.html
Copyright © 2011-2022 走看看