• 设计模式抽象工厂C++实现


    /*********************************
    *设计模式--抽象工厂实现
    *C++语言
    *Author:WangYong
    *Blog:http://www.cnblogs.com/newwy
    ********************************/
    
    #include <iostream>
    using namespace std;
    
    class AbstractProductA
    {
    	public:
    	virtual ~AbstractProductA() = 0;
    	AbstractProductA(){};
    };
    AbstractProductA::~AbstractProductA(){}
    
    class ProductA1:public AbstractProductA
    {
    	public:
    		ProductA1(){cout<<"ProductA1"<<endl;}	
    };
    
    class ProductA2:public AbstractProductA
    {
    	public:
    		ProductA2(){cout<<"ProductA2"<<endl;}	
    };
    
    class AbstractProductB
    {
    	public:
    	virtual ~AbstractProductB() = 0;
    	AbstractProductB(){};
    };
    AbstractProductB::~AbstractProductB(){}
    
    class ProductB1:public AbstractProductB
    {
    	public:
    		ProductB1(){cout<<"ProductB1"<<endl;}	
    };
    
    class ProductB2:public AbstractProductB
    {
    	public:
    		ProductB2(){cout<<"ProductB2"<<endl;}	
    };
    
    class AbstractFactory
    {
    	public:
    	virtual ~AbstractFactory();
    	virtual AbstractProductA *CreateProductA() = 0;
    	virtual AbstractProductB *CreateProductB() = 0;
    };
    AbstractFactory::~AbstractFactory(){}
    class ConcreteFactory1:public AbstractFactory
    {
    	public:
    	ConcreteFactory1(){cout<<"ConcreteFactory1..."<<endl;}
    	AbstractProductA *CreateProductA()  {return new ProductA1();}
    	AbstractProductB *CreateProductB()  {return new ProductB1();}
    };
    
    class ConcreteFactory2:public AbstractFactory
    {
    	public:
    	ConcreteFactory2(){cout<<"ConcreteFactory2..."<<endl;}
    	AbstractProductA *CreateProductA(){return new ProductA2();}
    	AbstractProductB *CreateProductB(){return new ProductB2();}
    };
    
    int main()
    {
    	AbstractFactory *cf1 = new ConcreteFactory1();
    	cf1->CreateProductA();cf1->CreateProductB();
    	
    	AbstractFactory *cf2 = new ConcreteFactory2();
    	cf2->CreateProductA();cf2->CreateProductB();
    	return 0;
    }
    
    
  • 相关阅读:
    sharepoint user field
    esata 安装 xp
    webservice without iis
    userdata
    png transparency
    使用命令行生成签名文件并用其对apk文件进行签名
    如何通过备份 Windows 7 “ 两个激活文件”实现重装操作系统后的自行激活
    SQLserver2008打不开的问题
    在mvc3中的@{}问题,mvc3做的有点小bug
    布局new分配 ,
  • 原文地址:https://www.cnblogs.com/newwy/p/1855194.html
Copyright © 2020-2023  润新知