zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    金盾视频高级加密系统 2016S VIP 注册版 高强度视频加密工具
    Webshell管理+网站后台管理+菜刀
    易 5.2 修正版+破解+完美支持Win8/7
    易5.1破解版+汉语编程
    UltraISOPE 9.6.2.3059简体中文注册版/单文件版+软碟通
    hfs网络文件服务器 2.3
    免费开通二级域名的论坛
    周星驰电影全集+BT种子下载+高清版MKV+周星驰系列电影合集
    DJ音乐盒-专注DJ
    EXE加载皮肤DLL
  • 原文地址:https://www.cnblogs.com/csxcode/p/3687325.html
Copyright © 2011-2022 走看看