Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2019604
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: C/C++

2009-01-29 10:36:51

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/********************************************************************
  2
  3created:
  4filename:
  5file path:
  6file base:
  7file ext:
  8author: 
  9
 10purpose:
 11*********************************************************************/
 12
 13
 14#include <iostream>
 15using namespace std;
 16
 17//the abstract implementation
 18class AbsProductImp 
 19{
 20public:
 21    virtual void DrawProductShape()=0;
 22};
 23//the abstract factory
 24class AbsFactory
 25{
 26public:
 27    virtual AbsProductImp * CreateAProductImp()=0;   //Create a real implement
 28};
 29
 30//the abstract product 
 31class AbsProduct
 32{
 33public:
 34    //virtual void DrawProductShape();
 35protected:
 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
 41
 42class AProductImp :public AbsProductImp                    //the real a product
 43{
 44public:
 45    void DrawProductShape()
 46    {
 47        cout<<"A product is describing itself; A is implement complete "<<endl;
 48    }
 49    
 50};
 51class BProductImp :public AbsProductImp                      //the real a product
 52{
 53public:
 54    virtual void DrawProductShape()
 55    {
 56        cout<<"B product is describing itself; B is implement complete "<<endl;
 57    }
 58    
 59};
 60
 61class AFactory :public AbsFactory                               //The factory that create A
 62{
 63public:
 64    virtual AbsProductImp * CreateAProductImp()
 65   {
 66        return new AProductImp;
 67    }
 68    
 69};
 70
 71class BFactory : public AbsFactory
 72{
 73public:
 74    virtual AbsProductImp * CreateAProductImp()
 75   {
 76        return new BProductImp;
 77    }
 78    
 79};
 80
 81class AProduct :public AbsProduct
 82{
 83public:
 84    AProduct()
 85   {
 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
 92class BProduct :public AbsProduct
 93{
 94public:
 95    BProduct()
 96    {
 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
103int main(int argc, char* argv[])
104{
105    AProduct  _a;  //Create An AProduct
106    BProduct  _b;  //Create A  BProduct
107    return 1;
108}
109
110
111
112

阅读(1531) | 评论(0) | 转发(0) |
0

上一篇:Class.forName 介绍

下一篇:Abstract Factory

给主人留下些什么吧!~~