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

创建型设计模式

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

介绍

在这里插入图片描述

​ —— 截图来自菜鸟教程

案例实现

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

在这里插入图片描述

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;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36413982/article/details/108306373