C++简单实现几种常用的设计模式

本文介绍几种常用的设计模式并给出C++实现。


1.单例模式

作用:保证一个类只有一个实例,并提供一个访问它的全局访问点,使得系统中只有唯一的一个对象实例

应用:常用于管理资源,如日志、线程池

实现要点:

在类中,要构造一个实例,就必须调用类的构造函数,并且为了保证全局只有一个实例,

防止在外部调用类的构造函数而构造实例,需要将构造函数的访问权限标记为private

同时阻止拷贝创建对象时赋值时拷贝对象,因此也将它们声明并权限标记为private;

另外,需要提供一个全局访问点,就需要在类中定义一个static函数,返回在类内部唯一构造的实例。

[cpp]  view plain  copy
  1. class Singleton{  
  2. public:  
  3.     static Singleton& getInstance(){  
  4.         static Singleton instance;  
  5.         return instance;  
  6.     }  
  7.     void printTest(){  
  8.         cout<<"do something"<<endl;  
  9.     }  
  10.   
  11. private:  
  12.     Singleton(){}//防止外部调用构造创建对象  
  13.     Singleton(Singleton const &singleton);//阻止拷贝创建对象  
  14.     Singleton& operator=(Singleton const &singleton);//阻止赋值对象  
  15.   
  16. };  
  17.   
  18. int main()  
  19. {  
  20.     Singleton &a=Singleton::getInstance();  
  21.     a.printTest();  
  22.     return 0;  
  23. }  
首先,构造函数声明成private的目的是只允许内部调用,getInstance()中的静态局部变量创建时调用,但不允许外部调用构造创建第二个实例;

然后,拷贝构造和拷贝赋值符是声明成了private而不给出定义,其目的是阻止拷贝,如果企图通过拷贝构造来创建第二个实例,编译器会报错。

阻止拷贝的另一种写法是声明后接一个"=delete",也能起到相同的作用(C++11)。


2.工厂模式

工厂模式包括三种:简单工厂模式、工厂方法模式、抽象工厂模式。

工厂模式的主要作用是封装对象的创建,分离对象的创建和操作过程,用于批量管理对象的创建过程,便于程序的维护和扩展。

(1)简单工厂模式

简单工厂是工厂模式最简单的一种实现,对于不同产品的创建定义一个工厂类,将产品的类型作为参数传入到工厂的创建函数,根据类型分支选择不同的产品构造函数。

[cpp]  view plain  copy
  1. //简单工厂模式  
  2. typedef enum ProductTypeTag  
  3. {  
  4.     TypeA,  
  5.     TypeB,  
  6.     TypeC  
  7. }PRODUCTTYPE;  
  8. class Product//产品抽象基类  
  9. {  
  10. public:  
  11.     virtual void Show() = 0;  
  12. };  
  13. class ProductA : public Product  
  14. {  
  15. public:  
  16.     void Show()  
  17.     {  
  18.         cout<<"I'm ProductA"<<endl;  
  19.     }  
  20. };  
  21. class ProductB : public Product  
  22. {  
  23. public:  
  24.     void Show()  
  25.     {  
  26.         cout<<"I'm ProductB"<<endl;  
  27.     }  
  28. };  
  29. class ProductC : public Product  
  30. {  
  31. public:  
  32.     void Show()  
  33.     {  
  34.         cout<<"I'm ProductC"<<endl;  
  35.     }  
  36. };  
  37. class Factory//工厂类  
  38. {  
  39. public:  
  40.     Product* CreateProduct(PRODUCTTYPE type)  
  41.     {  
  42.         switch (type)  
  43.         {  
  44.         case TypeA:  
  45.             return new ProductA();  
  46.   
  47.         case TypeB:  
  48.             return new ProductB();  
  49.   
  50.         case TypeC:  
  51.             return new ProductC();  
  52.   
  53.         default:  
  54.             return NULL;  
  55.         }  
  56.     }  
  57. };  
  58.   
  59. int main()  
  60. {  
  61.     Factory productCreator;  
  62.     Product *productA=productCreator.CreateProduct(TypeA);  
  63.     Product *productB=productCreator.CreateProduct(TypeB);  
  64.     Product *productC=productCreator.CreateProduct(TypeC);  
  65.     productA->Show();  
  66.     productB->Show();  
  67.     productC->Show();  
  68.     if(productA){  
  69.         delete productA;  
  70.         productA=NULL;  
  71.     }  
  72.     if(productB){  
  73.         delete productB;  
  74.         productB=NULL;  
  75.     }  
  76.     if(productC){  
  77.         delete productC;  
  78.         productC=NULL;  
  79.     }  
  80.     return 0;  
  81. }  
(2)工厂方法模式

其实这才是正宗的工厂模式,简单工厂模式只是一个简单的对创建过程封装。工厂方法模式在简单工厂模式的基础上增加对工厂的基类抽象,不同的产品创建采用不同的工厂创建(从工厂的抽象基类派生),这样创建不同的产品过程就由不同的工厂分工解决:FactoryA专心负责生产ProductA,FactoryB专心负责生产ProductB,FactoryA和FactoryB之间没有关系;如果到了后期,如果需要生产ProductC时,我们则可以创建一个FactoryC工厂类,该类专心负责生产ProductC类产品。

该模式相对于简单工厂模式的优势在于:便于后期产品种类的扩展。

[cpp]  view plain  copy
  1. //工厂方法模式  
  2. typedef enum ProductTypeTag  
  3. {  
  4.     TypeA,  
  5.     TypeB,  
  6.     TypeC  
  7. }PRODUCTTYPE;  
  8. class Product//产品抽象基类  
  9. {  
  10. public:  
  11.     virtual void Show() = 0;  
  12. };  
  13. class ProductA : public Product  
  14. {  
  15. public:  
  16.     void Show()  
  17.     {  
  18.         cout<<"I'm ProductA"<<endl;  
  19.     }  
  20. };  
  21. class ProductB : public Product  
  22. {  
  23. public:  
  24.     void Show()  
  25.     {  
  26.         cout<<"I'm ProductB"<<endl;  
  27.     }  
  28. };  
  29.   
  30. class Factory//工厂类  
  31. {  
  32. public:  
  33.     virtual Product *createProduct()=0;  
  34. };  
  35. class FactoryA:public Factory{  
  36. public:  
  37.     Product *createProduct(){  
  38.         return new ProductA();  
  39.     }  
  40. };  
  41. class FactoryB:public Factory{  
  42. public:  
  43.     Product *createProduct(){  
  44.         return new ProductB();  
  45.     }  
  46. };  
  47. class FactoryC:public Factory{  
  48. public:  
  49.     Product *createProduct(){  
  50.         return new ProductC();  
  51.     }  
  52. };  
  53.   
  54. int main()  
  55. {  
  56.         Factory *factoryA=new FactoryA();  
  57.         Product *productA = factoryA->createProduct();  
  58.         productA->Show();  
  59.         Factory *factoryB=new FactoryB();  
  60.         Product *productB = factoryB->createProduct();  
  61.         productB->Show();  
  62.         if (factoryA)  
  63.         {  
  64.             delete factoryA;  
  65.             factoryA = NULL;  
  66.         }  
  67.         if (factoryB)  
  68.         {  
  69.             delete factoryB;  
  70.             factoryB = NULL;  
  71.         }  
  72.         if (productA)  
  73.         {  
  74.             delete productA;  
  75.             productA = NULL;  
  76.         }  
  77.         if (productB)  
  78.         {  
  79.             delete productB;  
  80.             productB = NULL;  
  81.         }  
  82.         return 0;  
  83. }  

(3)抽象工厂模式

抽象工厂模式对工厂方法模式进行了更加一般化的描述。工厂方法模式适用于产品种类结构单一的场合,为一类产品提供创建的接口;而抽象工厂方法适用于产品种类结构多的场合,就是当具有多个抽象产品类型时,抽象工厂便可以派上用场。

抽象工厂模式更适合实际情况,受生产线所限,让低端工厂生产不同种类的低端产品,高端工厂生产不同种类的高端产品。

[cpp]  view plain  copy
  1. //抽象工厂模式  
  2. class ProductA  
  3. {  
  4. public:  
  5.     virtual void Show() = 0;  
  6. };  
  7. class ProductA1 : public ProductA//A类低端产品  
  8. {  
  9. public:  
  10.     void Show()  
  11.     {  
  12.         cout<<"I'm ProductA1"<<endl;  
  13.     }  
  14. };  
  15. class ProductA2 : public ProductA//A类高端产品  
  16. {  
  17. public:  
  18.     void Show()  
  19.     {  
  20.         cout<<"I'm ProductA2"<<endl;  
  21.     }  
  22. };  
  23. class ProductB  
  24. {  
  25. public:  
  26.     virtual void Show() = 0;  
  27. };  
  28. class ProductB1 : public ProductB//B类低端产品  
  29. {  
  30. public:  
  31.     void Show()  
  32.     {  
  33.         cout<<"I'm ProductB1"<<endl;  
  34.     }  
  35. };  
  36. class ProductB2 : public ProductB//B类高端产品  
  37. {  
  38. public:  
  39.     void Show()  
  40.     {  
  41.         cout<<"I'm ProductB2"<<endl;  
  42.     }  
  43. };  
  44.   
  45. class Factory  
  46. {  
  47. public:  
  48.     virtual ProductA *CreateProductA() = 0;  
  49.     virtual ProductB *CreateProductB() = 0;  
  50. };  
  51.   
  52. class Factory1 : public Factory//1号工厂用于生产低端产品  
  53. {  
  54. public:  
  55.     ProductA *CreateProductA()  
  56.     {  
  57.         return new ProductA1();  
  58.     }  
  59.   
  60.     ProductB *CreateProductB()  
  61.     {  
  62.         return new ProductB1();  
  63.     }  
  64. };  
  65.   
  66. class Factory2 : public Factory//2号工厂用于生产高端产品  
  67. {  
  68.     ProductA *CreateProductA()  
  69.     {  
  70.         return new ProductA2();  
  71.     }  
  72.   
  73.     ProductB *CreateProductB()  
  74.     {  
  75.         return new ProductB2();  
  76.     }  
  77. };  
  78.   
  79. int main()  
  80. {  
  81.     Factory *factory1 = new Factory1();  
  82.     ProductA *productA1 = factory1->CreateProductA();  
  83.     ProductB *productB1 = factory1->CreateProductB();  
  84.     productA1->Show();  
  85.     productB1->Show();  
  86.   
  87.     Factory *factory2 = new Factory2();  
  88.     ProductA *productA2 = factory2->CreateProductA();  
  89.     ProductB *productB2 = factory2->CreateProductB();  
  90.     productA2->Show();  
  91.     productB2->Show();  
  92.   
  93.     if (factory1)  
  94.     {  
  95.         delete factory1;  
  96.         factory1 = NULL;  
  97.     }  
  98.     if (productA1)  
  99.     {  
  100.         delete productA1;  
  101.         productA1= NULL;  
  102.     }  
  103.     if (productB1)  
  104.     {  
  105.         delete productB1;  
  106.         productB1 = NULL;  
  107.     }  
  108.     if (factory2)  
  109.     {  
  110.         delete factory2;  
  111.         factory2 = NULL;  
  112.     }  
  113.     if (productA2)  
  114.     {  
  115.         delete productA2;  
  116.         productA2 = NULL;  
  117.     }  
  118.     if (productB2)  
  119.     {  
  120.         delete productB2;  
  121.         productB2 = NULL;  
  122.     }  
  123. }  

猜你喜欢

转载自blog.csdn.net/m0_37584483/article/details/80348404