设计模式学习 — 抽象工厂模式

   日期:2020-08-31     浏览:152    评论:0    
核心提示:创建型设计模式围绕一个超级工厂创建其他工厂介绍​ —— 截图来自菜鸟教程案例实现日常吃的东西都统称为食物,食物中又分成很多类,如蔬菜、水果等,蔬菜中又有胡萝卜、土豆、西红柿等等,水果则有苹果、香蕉、芒果等等。1、蔬菜和水果抽象基类class Vegetable{publi

创建型设计模式

围绕一个超级工厂创建其他工厂

介绍

​ —— 截图来自菜鸟教程

案例实现

日常吃的东西都统称为食物,食物中又分成很多类,如蔬菜、水果等,蔬菜中又有胡萝卜、土豆、西红柿等等,水果则有苹果、香蕉、芒果等等。

1、蔬菜和水果抽象基类

class Vegetable
{
public:
    virtual void showName(void) = 0;
};

class Fruit
{
public:
    virtual void showName(void) = 0;
};

2、水果类和蔬菜类的子类实现

class Tomato : public Vegetable
{
public:
    void showName();
};

void Tomato::showName()
{
    cout << "Tomato" << endl;
}

class Apple : public Fruit
{
public:
    void showName();
};

void Apple::showName()
{
    cout << "apple" << endl;
}

其他蔬菜和水果实现都一样。

3、实现抽象的食物工厂

class AbstractFoodFactory
{
public:
    virtual Vegetable *getVegetable(string type) = 0;
    virtual Fruit *getFruit(string type) = 0;
};

两个纯虚函数分别对应创建蔬菜和水果。

4、实现水果和蔬菜工厂

class FruitFactory : public AbstractFoodFactory
{
public:
    Fruit *getFruit(string type);
    Vegetable *getVegetable(string type);
};

Fruit *FruitFactory::getFruit(string type)
{
    if (type == "Apple")
    {
        return new Apple;
    }
    else if (type == "Banana")
    {
        return new Banana;
    }
    else if (type == "Mango")
    {
        return new Mango;
    }

    return NULL;
}
//因为是纯虚函数,所以也要实现不然会报错
Vegetable *FruitFactory::getVegetable(string type)
{
}

class VegetableFactory : public AbstractFoodFactory
{
public:
    Vegetable *getVegetable(string type);
    Fruit *getFruit(string type);
};

Vegetable * VegetableFactory::getVegetable(string type)
{
    if (type == "Tomato")
    {
        return new Tomato();
    }
    else if (type == "Potato")
    {
        return new Potato();
    }
    else if (type == "Carrot")
    {
        return new Carrot();
    }
    return NULL;
}

Fruit *VegetableFactory::getFruit(string type)
{
}

5、实现工厂生成器


class FoodFactoryProducer
{
public:
    static AbstractFoodFactory *getFactory(string type);

};

AbstractFoodFactory *FoodFactoryProducer::getFactory(string type)
{
    if (type == "Vegetable")
    {
        return new VegetableFactory(); //创建蔬菜工厂
    }
    else if (type == "Fruit")
    {
        return new FruitFactory();   //创建水果工厂
    }
    return NULL;
}

测试

int main()
{
    AbstractFoodFactory *vegetableFac = 		             					        FoodFactoryProducer::getFactory("Vegetable");    //创建蔬菜工厂
    Vegetable *aTomato = vegetableFac->getVegetable("Tomato");   //西红柿
    aTomato->showName();

    Vegetable *aPotato = vegetableFac->getVegetable("Potato"); //土豆
    aPotato->showName();

    Vegetable *aCarrot = vegetableFac->getVegetable("Carrot"); //胡萝卜
    aCarrot->showName();

    cout << endl;

    AbstractFoodFactory *fruitFac = FoodFactoryProducer::getFactory("Fruit"); //创建水果工厂
    Fruit *aMango = fruitFac->getFruit("Mango");              //芒果
    aMango->showName();

    Fruit *aApple = fruitFac->getFruit("Apple");                //苹果
    aApple->showName();

    Fruit *aBanana = fruitFac->getFruit("Banana");              //香蕉
    aBanana->showName();

    delete aTomato;
    delete aPotato;
    delete aCarrot;
    delete vegetableFac;

    delete aMango;
    delete aApple;
    delete aBanana;
    delete fruitFac;

    return 0;
}

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服