• Factory 模式


     1 ////////////Factory.h////////////////////////
     2 #ifndef _FACTORY_H_
     3 #define _FACTORY_H_
     4 
     5 class Product ;
     6 
     7 class Factory
     8 {
     9 public:
    10     virtual ~Factory() = 0 ;
    11 
    12     virtual  Product* CreatProduct() = 0;
    13 
    14 protected:
    15     Factory();
    16 private:
    17 };
    18 
    19 class ConcreteFactory : public Factory 
    20 {
    21 public:
    22     ~ConcreteFactory();
    23     ConcreteFactory();
    24     Product* CreatProduct();
    25 protected:
    26 private:
    27 };
    28 
    29 #endif
     1 ////////////Factory.cpp////////////////////////
     2 #include "Factory.h"
     3 #include "Product.h"
     4 
     5 #include <iostream>
     6 using namespace std;
     7 
     8 Factory::Factory()
     9 {
    10 
    11 }
    12 
    13 Factory::~Factory()
    14 {
    15 
    16 }
    17 
    18 ConcreteFactory::ConcreteFactory()
    19 {
    20     cout<<"ConcreteFactory....."<<endl; 
    21 }
    22 
    23 ConcreteFactory::~ConcreteFactory()
    24 {
    25 
    26 }
    27 
    28 Product* ConcreteFactory::CreatProduct()
    29 {
    30     return new ConcreteProduct();
    31 }
     1 ////////////Product.h////////////////////////
     2 #ifndef  _PRODUCT_H_
     3 #define  _PRODUCT_H_
     4 class Product
     5 {
     6 public:
     7     virtual ~Product()=0;
     8 
     9 protected:
    10     Product();
    11 private:
    12 };
    13 
    14 
    15 class ConcreteProduct : public Product
    16 {
    17 public:
    18     virtual ~ConcreteProduct();
    19     ConcreteProduct();
    20 protected:
    21 private:
    22 };
    23 
    24 
    25 #endif 
     1 ////////////Product.cpp////////////////////////
     2 #include "Product.h"
     3 #include <iostream>
     4 using namespace std;
     5 
     6 Product::Product()
     7 {
     8 
     9 }
    10 
    11 Product::~Product()
    12 {
    13 
    14 }
    15 
    16 ConcreteProduct::ConcreteProduct()
    17 {
    18     cout<<"ConcreteProduct"<<endl ;
    19 }
    20 
    21 ConcreteProduct::~ConcreteProduct()
    22 {
    23 
    24 }
     1 ////////////main.cpp////////////////////////
     2 #include "Factory.h"
     3 #include "Product.h"
     4 
     5 #include <iostream>
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     Factory *fac  = new ConcreteFactory() ;
    11 
    12     Product *p = fac->CreatProduct();
    13     getchar();
    14     return 0 ;
    15 }
  • 相关阅读:
    Java spring自查笔记
    C#设置IP地址 用WMI
    idea换成护眼色
    WPF布局的6种面板
    spring注解
    c# 播放mp3
    Python 提示 “No matching distribution found for MySQLdb”
    Python3链接数据库报错:Connection.__init__() takes 1 positional argument but 5 positional arguments (and 1 keywordonly argument) were given
    安装Mysql数据库及配置相关环境变量
    Python中文件命名的注意事项
  • 原文地址:https://www.cnblogs.com/csxcode/p/3687325.html
Copyright © 2020-2023  润新知