The Gof has 23 design patterns , but in most of the design books , the E.G is written in Java , So from now on , I will write about 23 articles to implement them using C++ , the design pattern itself is discussible , so welcome everyone to give his excelent idea to me , My QQ: 286259397 And My MSN :
cxl82116@msn.com.
Welcome every one to express his ideas!
This is the First Arcticle: ABSTRACT FACTORY
Here the theory goes:
/*The theory basis :
While using abstract factory , the situation will be almost like this:
we can create a real product from the abstract product , there are many series of product ,
and the all have the similar property and operations , the operations can be different inner,
but all the same interface outer;
What's need?
An abstract factory , and many real factory derived from this factory create real products
An abstract product , any many real product derived from this proudct ;
An abstract Interface , and many real Interface that realize the interface
*/
Here the code goes:
1![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
2
created: 2007/04/18
3
created: 18:4:2007 0:42
4
filename: C:\testvc6\TestStll\TestStll.cpp
5
file path: C:\testvc6\TestStll
6
file base: TestStll
7
file ext: cpp
8
author: Chang xinglong(King.C)
9![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
10
purpose: Abstract Factory
11
*********************************************************************/
12![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
13![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
14
#include <iostream>
15
using namespace std;
16![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
17
//the abstract implementation
18
class AbsProductImp
19![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
20
public:
21
virtual void DrawProductShape()=0;
22
};
23
//the abstract factory
24
class AbsFactory
25![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
26
public:
27
virtual AbsProductImp * CreateAProductImp()=0; //Create a real implement
28
};
29![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
30
//the abstract product
31
class AbsProduct
32![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
33
public:
34
//virtual void DrawProductShape();
35
protected:
36
AbsProductImp * _imp; //implement pointer , it's decide which implement be execute
37
AbsFactory * _Fac;// this pointer will point to the real factory
38
39
};
40
// Derived begain [4/18/2007 Chang Xinglong(King.C)]
41![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
42
class AProductImp :public AbsProductImp //the real a product
43![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
44
public:
45
void DrawProductShape()
46![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
cout<<"A product is describing itself
A is implement complete "<<endl;
48
}
49
50
};
51
class BProductImp :public AbsProductImp //the real a product
52![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
53
public:
54
virtual void DrawProductShape()
55![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
56
cout<<"B product is describing itself
B is implement complete "<<endl;
57
}
58
59
};
60
// Derived the Factory [4/18/2007 Chang Xinglong(King.C)]
61
class AFactory :public AbsFactory//The factory that create A
62![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
63
public:
64
virtual AbsProductImp * CreateAProductImp()
65![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
66
return new AProductImp;
67
}
68
69
};
70![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
71
class BFactory : public AbsFactory
72![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
73
public:
74
virtual AbsProductImp * CreateAProductImp()
75![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
76
return new BProductImp;
77
}
78
79
};
80![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
81
class AProduct :public AbsProduct
82![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
83
public:
84
AProduct()
85![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
86
_Fac=new AFactory; // specific the AFactory to create A
87
_imp=_Fac->CreateAProductImp(); // polymorphism , select the product implement
88
_imp->DrawProductShape(); //construct it self
89
}
90
};
91![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
92
class BProduct :public AbsProduct
93![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
94
public:
95
BProduct()
96![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
97
_Fac=new BFactory; // specific the BFactory to create B
98
_imp=_Fac->CreateAProductImp(); // polymorphism , select the product implement
99
_imp->DrawProductShape(); //construct it self
100
}
101
};
102
// Derived the products [4/18/2007 Administrator]
103
int main(int argc, char* argv[])
104![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
105
AProduct _a; //Create An AProduct
106
BProduct _b; //Create A BProduct
107
return 1;
108
}
109![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
110![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
111![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
112![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)