zoukankan      html  css  js  c++  java
  • 设计模式之Factory模式(C++)

    Factory模式具有两大重要的功能:

    (1)、定义创建对象的接口,封装了对象的创建;

    (2)、使具体化类工作延迟到了子类中。

    //Product.h
    #ifndef _PRODUCT_H_
    #define _PRODUCT_H_
    
    class Product
    {
    public:
          virtual ~Product();
    protected:
           Product();
    private:
       
    };
    
    class ConcreteProduct:public Product
    {
    public:
        ConcreteProduct();
        ~ConcreteProduct();
    protected:
    
    private:
    };
    #endif //_PRODUCT_H_
    //Product.cpp
    #include "Product.h"
    #include <iostream>
    using namespace std;
    
    Product::Product()
    {}
    Product::~Product()
    {}
    ConcreteProduct::ConcreteProduct()
    {}
    ConcreteProduct::~ConcreteProduct()
    {}
    //Factory.h
    #ifndef _FACTORY_H_
    #define _FACTORY_H_
    
    class Product;
    class Factory
    {
    public:
        virtual ~Factory();
        virtual Product *CreateProduct() = 0; 
    protected:
        void Factory();
    private:
    };
    
    class ConcreteFactory:public Factory
    {
    public:
        ConcreteFactory();
        ~ConcreteFactory();
        Product * CreateProduct();
    protected:
    private:    
    };
    #endif //_FACTORY_H_
    //Fatory.cpp
    
    #include "Factory.h"
    #include "Product.h"
    
    #include <iostream>
    using namespace std;
    
    Factory::Factory()
    {}
    Factory::~Factory()
    {}
    
    ConcreteFactory::ConcreteFactory()
    {}
    ConcreteFactory::ConcreteFactory()
    {}
    
    Product ConcreteFactory::CreateProduct()
    {
        ConcreteProduct cproduct = new ConcreteProduct;
        return product;
    }
    //main.cpp
    
    #include "Factory.h"
    #include "Product.h"
    
    #include <iostream>
    using namespace std;
    
    int main(int argc,char *argv[])
    {
        Factory *fac = new ConcreteFactory;
        Product *p = fac->CreateProduct();
    
        return 0;
    }

  • 相关阅读:
    MYSQL GDB 崩溃调试
    MYSQL GDB SHELL
    OOW 2015 MYSQL
    DTCC:MySQl核心代码开发经验揭示
    东东糖博客MYSQL
    perf 函数调用性能(函数流程图)
    Percona-Toolkit学习之安装和配置
    Upgrading Directly from MySQL 5.0 to 5.7 using an ‘In Place’ Upgrade
    在线遥感影像与地图集数据下载搜集
    墨卡托投影、高斯-克吕格投影、UTM投影及我国分带方法
  • 原文地址:https://www.cnblogs.com/tslDream/p/4456397.html
Copyright © 2011-2022 走看看