Chinaunix首页 | 论坛 | 博客
  • 博客访问: 863178
  • 博文数量: 156
  • 博客积分: 6553
  • 博客等级: 准将
  • 技术积分: 3965
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 18:36
文章存档

2012年(3)

2011年(43)

2010年(110)

分类: C/C++

2011-03-21 12:04:42


一个工厂负责一个产品
编程实例:
// this is a example for test factory method
#include

using namespace std;

//abstract food
class baseFish
{
    public:
//        virtual ~baseFish(){};
        virtual void getFood(){};
};

class steamedFish:public baseFish
{
    virtual void getFood()
    {
        cout << "this steamed fish" <
    };
};

class roastFish:public baseFish
{
        virtual void getFood()
        {
               cout << "this roast fish" <
        };
};


//abstract factory
class baseFactory
{
public:
//    virtual ~baseFactory(){}
    virtual baseFish* createFood(){};
};

class steamedFishFactory:public baseFactory
{
public:
    baseFish * createFood()
    {
        return new steamedFish();
    };
};

class roastFishFactory:public baseFactory
{
    public:
        baseFish * createFood()
        {
            return new roastFish;
        };
};

int main()
{
    baseFactory *factory = new steamedFishFactory;
    baseFish *fish = factory->createFood();
    fish->getFood();

    return 0;
}
抽象类不可以定义对象,可以定义指针

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

上一篇:简单工厂模式

下一篇:抽象工厂模式

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