zoukankan      html  css  js  c++  java
  • 设计模式抽象工厂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;
    }
    
    
  • 相关阅读:
    常见浏览器的兼容问题以及解决方案 (仅供参考)
    了解浏览器如何工作—渲染引擎1
    维基百科公式不能正常显示
    IDL读取fits文件
    卷积,系统
    硬盘被占用无法拔出问题解决
    Faster RCNN 改进论文及资料
    Pycharm 使用问题一览
    java eclipse 使用随笔
    Images
  • 原文地址:https://www.cnblogs.com/newwy/p/1855194.html
Copyright © 2011-2022 走看看