码迷,mamicode.com
首页 > 其他好文 > 详细

AbstractFactory Pattern

时间:2015-07-17 13:49:31      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

AbstractFactory模式用来解决这类问题:要创建一组相关或者相互依赖的对象。

AbstractFactory Pattern结构图

技术分享

实现:

技术分享
 1 #ifndef _PRODUCT_H_ 
 2 #define _PRODUCT_H_
 3 class AbstractProductA 
 4 { 
 5 public: 
 6     virtual ~AbstractProductA();
 7 protected: 
 8     AbstractProductA();
 9 private:
10 };
11 
12 class AbstractProductB 
13 { 
14 public: 
15     virtual ~AbstractProductB();
16 protected: 
17     AbstractProductB();
18 private:
19 };
20 
21 class ProductA1:public AbstractProductA 
22 { 
23 public: 
24     ProductA1();
25     ~ProductA1();
26 protected:
27 private:
28 }
29 
30 class ProductA2:public AbstractProductA 
31 {
32 public: 
33     ProductA2();
34     ~ProductA2();
35 protected:
36 private:
37 };
38 
39 class ProductB1:public AbstractProductB 
40 { 
41 public: 
42     ProductB1();
43     ~ProductB1();
44 protected:
45 private:
46 };
47 
48 class ProductB2:public AbstractProductB 
49 { 
50 public: 
51     ProductB2();
52     ~ProductB2();
53 protected:
54 private:
55 };
56 
57 #endif
Product.h
技术分享
 1 #include "Product.h"
 2 #include <iostream> 
 3 using namespace std;
 4 
 5 AbstractProductA::AbstractProductA() 
 6 {
 7 
 8 }
 9 AbstractProductA::~AbstractProductA() 
10 {
11 
12 }
13 AbstractProductB::AbstractProductB() 
14 {
15 
16 }
17 AbstractProductB::~AbstractProductB() 
18 {
19 
20 }
21 ProductA1::ProductA1() 
22 { 
23     cout<<"ProductA1..."<<endl; 
24 }
25 ProductA1::~ProductA1() 
26 {
27 
28 }
29 ProductA2::ProductA2() 
30 { 
31     cout<<"ProductA2..."<<endl; 
32 }
33 ProductA2::~ProductA2() 
34 {
35 
36 }
37 ProductB1::ProductB1()
38 {
39     cout<<"ProductB1..."<<endl;
40 }
41 ProductB1::~ProductB1() 
42 {
43 
44 }
45 ProductB2::ProductB2() 
46 { 
47     cout<<"ProductB2..."<<endl;
48 }
49 ProductB2::~ProductB2() 
50 {
51 
52 }
Product.cpp
技术分享
 1 #ifndef _ABSTRACTFACTORY_H_ 
 2 #define _ABSTRACTFACTORY_H_
 3 
 4 class AbstractProductA; 
 5 class AbstractProductB;
 6 
 7 class AbstractFactory 
 8 { 
 9 public: 
10     virtual ~AbstractFactory();
11     virtual AbstractProductA* CreateProductA() = 0;
12     virtual AbstractProductB* CreateProductB() = 0;
13 protected: 
14     AbstractFactory();
15 private:
16 };
17 
18 class ConcreteFactory1:public AbstractFactory 
19 { 
20 public:
21     ConcreteFactory1();
22     ~ConcreteFactory1();
23     AbstractProductA* CreateProductA();
24     AbstractProductB* CreateProductB();
25 protected:
26 private:
27 };
28 
29 class ConcreteFactory2:public AbstractFactory 
30 { 
31 public: 
32     ConcreteFactory2();
33     ~ConcreteFactory2();
34     AbstractProductA* CreateProductA();
35     AbstractProductB* CreateProductB();
36 protected:
37 private:
38 }; 
39 
40 #endif
AbstractFactory.h
技术分享
 1 #include "AbstractFactory.h" 
 2 #include "Product.h"
 3 #include <iostream> 
 4 using namespace std;
 5 
 6 AbstractFactory::AbstractFactory() 
 7 {
 8 
 9 }
10 AbstractFactory::~AbstractFactory()
11 {
12 
13 }
14 ConcreteFactory1::ConcreteFactory1() 
15 {
16 
17 }
18 ConcreteFactory1::~ConcreteFactory1() 
19 {
20 
21 }
22 AbstractProductA* ConcreteFactory1::CreateProductA() 
23 { 
24     return new ProductA1(); 
25 }
26 AbstractProductB* ConcreteFactory1::CreateProductB() 
27 { 
28     return new ProductB1(); 
29 }
30 ConcreteFactory2::ConcreteFactory2() 
31 {
32 
33 }
34 ConcreteFactory2::~ConcreteFactory2() 
35 {
36 
37 }
38 AbstractProductA* ConcreteFactory2::CreateProductA() 
39 { 
40     return new ProductA2(); 
41 }
42 AbstractProductB* ConcreteFactory2::CreateProductB() 
43 { 
44     return new ProductB2(); 
45 }
AbstractFactory.cpp

AbstractFactory Pattern

标签:

原文地址:http://www.cnblogs.com/programmer-wfq/p/4652154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!