• AbstractFactory 模式


     1 ///////////////////////Product.h//////////////
     2 #ifndef _PRODUCT_H_
     3 #define _PRODUCT_H_ 
     4 class AbstractProductA
     5 {
     6 public:
     7     virtual ~AbstractProductA();
     8 protected:
     9     AbstractProductA();
    10 private:
    11 };
    12 
    13 
    14 class AbstractProductB
    15 {
    16 public:
    17     virtual ~AbstractProductB();
    18 protected:
    19     AbstractProductB();
    20 private:
    21 };
    22 
    23 class ProductA1 : public AbstractProductA
    24 {
    25 public:
    26     ~ProductA1();
    27     ProductA1();
    28 protected:    
    29 private:
    30 };
    31 
    32 class ProductA2 : public AbstractProductA
    33 {
    34 public:
    35     ~ProductA2();
    36     ProductA2();
    37 protected:    
    38 private:
    39 };
    40 
    41 class ProductB1 : public AbstractProductB
    42 {
    43 public:
    44     ~ProductB1();
    45     ProductB1();
    46 protected:    
    47 private:
    48 };
    49 
    50 class ProductB2 : public AbstractProductB
    51 {
    52 public:
    53     ~ProductB2();
    54     ProductB2();
    55 protected:    
    56 private:
    57 };
    58 
    59 #endif
     1 ///////////////////////Product.cpp//////////////
     2 #include "Product.h"
     3 #include <iostream>
     4 using namespace std;
     5 
     6 AbstractProductA::~AbstractProductA()
     7 {
     8 
     9 }
    10 AbstractProductA::AbstractProductA()
    11 {
    12 
    13 }
    14 AbstractProductB::~AbstractProductB()
    15 {
    16 
    17 }
    18 AbstractProductB::AbstractProductB()
    19 {
    20 
    21 }
    22 
    23 ProductA1::~ProductA1()
    24 {
    25 
    26 }
    27 ProductA1::ProductA1()
    28 {
    29     cout<<"ProductA1"<<endl;
    30 }
    31 
    32 ProductA2::~ProductA2()
    33 {
    34 
    35 }
    36 ProductA2::ProductA2()
    37 {
    38     cout<<"ProductA2"<<endl;
    39 }
    40 
    41 ProductB1::~ProductB1()
    42 {
    43 
    44 }
    45 ProductB1::ProductB1()
    46 {
    47     cout<<"ProductB1"<<endl;
    48 }
    49 ProductB2::~ProductB2()
    50 {
    51 
    52 }
    53 ProductB2::ProductB2()
    54 {
    55     cout<<"ProductB2"<<endl;
    56 }
     1 ///////////////////////Factory.h//////////////
     2 #ifndef _FACTORY_H_
     3 #define _FACTORY_H_
     4 
     5 class AbstractProductA;
     6 class AbstractProductB;
     7 
     8 class AbstractFactory
     9 {
    10 public:
    11     virtual ~AbstractFactory();
    12     virtual AbstractProductA* CreatProductA()=0;
    13     virtual AbstractProductB* CreatProductB()=0;
    14 protected:
    15     AbstractFactory();
    16 private:
    17 };
    18 
    19 class ConcreteFactory1 : public AbstractFactory
    20 {
    21 public:
    22     ~ConcreteFactory1();
    23     ConcreteFactory1();
    24     AbstractProductA* CreatProductA();
    25     AbstractProductB* CreatProductB();
    26 protected:
    27 private:
    28 };
    29 
    30 
    31 class ConcreteFactory2 : public AbstractFactory
    32 {
    33 public:
    34     ~ConcreteFactory2();
    35     ConcreteFactory2();
    36     AbstractProductA* CreatProductA();
    37     AbstractProductB* CreatProductB();
    38 protected:
    39 private:
    40 };
    41 
    42 #endif
     1 ///////////////////////Factory.cpp//////////////
     2 #include "Factory.h"
     3 #include "Product.h"
     4 
     5 AbstractFactory::~AbstractFactory()
     6 {
     7 
     8 }
     9 AbstractFactory::AbstractFactory()
    10 {
    11 
    12 }
    13 
    14 ConcreteFactory1::~ConcreteFactory1(){}
    15 ConcreteFactory1::ConcreteFactory1(){}
    16 AbstractProductA*  ConcreteFactory1::CreatProductA()
    17 {
    18     return new ProductA1();
    19 }
    20 
    21 AbstractProductB*  ConcreteFactory1::CreatProductB()
    22 {
    23     return new ProductB1();
    24 }
    25 
    26 
    27 ConcreteFactory2::~ConcreteFactory2(){}
    28 ConcreteFactory2::ConcreteFactory2(){}
    29 AbstractProductA*  ConcreteFactory2::CreatProductA()
    30 {
    31     return new ProductA2();
    32 }
    33 
    34 AbstractProductB*  ConcreteFactory2::CreatProductB()
    35 {
    36     return new ProductB2();
    37 }
     1 ///////////////////////main.cpp//////////////
     2 #include "Factory.h"
     3 #include <iostream>
     4 
     5 int main()
     6 {
     7     AbstractFactory* fac1 = new ConcreteFactory1();
     8     fac1->CreatProductA();
     9     fac1->CreatProductB();
    10 
    11     AbstractFactory* fac2 = new ConcreteFactory2();
    12     fac2->CreatProductA();
    13     fac2->CreatProductB();
    14 
    15     getchar();
    16     
    17     return 0;
    18 }
  • 相关阅读:
    jdbc连接Mysql数据库
    测试ibatis3连接数据
    dbcp参数配置
    努力---是永远且持久的行为
    android---textview控件学习笔记之显示表情图片和文本(二)
    android---textview控件学习笔记之显示文本(一)
    程序员的要求
    android的adb命令中,pm,am的使用
    完成celery简单发送注册邮件
    培养代码逻辑
  • 原文地址:https://www.cnblogs.com/csxcode/p/3687539.html
Copyright © 2020-2023  润新知