码迷,mamicode.com
首页 > 编程语言 > 详细

抽象工厂模式之C++实现

时间:2014-06-24 14:38:48      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   color   get   

 

 

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

//Product
class Water
{
public:
    virtual void AddWater() = 0;
};

class Flour
{
public:
    virtual void AddFlour() = 0;
};

class BreadWater : public Water
{
public:
    void AddWater()
    {
        cout << "ÏòÃæ°ü»úÆ÷ÖÐÌí¼ÓË®" << endl;
    }
};

class BreadFlour : public Flour
{
public:
    void AddFlour()
    {
        cout << "ÏòÃæ°ü»úÆ÷ÖÐÌí¼ÓÃæ·Û" << endl;
    }
};

class SteamedBreadWater : public Water
{
public:
    void AddWater()
    {
        cout << "ÏòÂøÍ·»úÆ÷ÖÐÌí¼ÓË®" << endl;
    }
};

class SteamedBreadFlour : public Flour
{
public:
    void AddFlour()
    {
        cout << "ÏòÂøÍ·»úÆ÷ÖÐÌí¼ÓÃæ·Û" << endl;
    }
};



//Factory
class Factory
{
public:
    virtual Water* GetWater() = 0;
    virtual Flour* GetFlour() = 0;
    virtual void GetProduct() = 0;
};

class BreadFactory : public Factory
{
public:
    Water* GetWater()
    {
        return new BreadWater;
    }

    Flour* GetFlour()
    {
        return new BreadFlour;
    }

    void GetProduct()
    {
        cout << "Ãæ°ü³ö¯!" << endl;
    }
};

class SteamedBreadFactory : public Factory
{
public:
    Water* GetWater()
    {
        return new SteamedBreadWater;
    }

    Flour* GetFlour()
    {
        return new SteamedBreadFlour;
    }

    void GetProduct()
    {
        cout << "ÂøÍ·³ö¯!" << endl;
    }
};
    

int main()
{
    Factory *pBreadFactory = new BreadFactory;
    pBreadFactory->GetFlour()->AddFlour();
    pBreadFactory->GetWater()->AddWater();
    pBreadFactory->GetProduct();

    Factory *pSteamedBreadFactory = new SteamedBreadFactory;
    pSteamedBreadFactory->GetFlour()->AddFlour();
    pSteamedBreadFactory->GetWater()->AddWater();
    pSteamedBreadFactory->GetProduct();

    free(pSteamedBreadFactory);
    free(pBreadFactory);
    return 0;
}

 

抽象工厂模式之C++实现,布布扣,bubuko.com

抽象工厂模式之C++实现

标签:style   class   blog   code   color   get   

原文地址:http://www.cnblogs.com/jingmoxukong/p/3805612.html

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