当前位置:网站首页>Factory mode

Factory mode

2022-04-23 20:46:00 baboon_ chen

The factory model is divided into 3 Kind of , The simple factory model 、 Factory method model 、 Abstract factory pattern .

One 、 Simple factory model

The simple factory pattern has a factory class , A product class . The factory class contains a method , You can choose to produce certain types of products . The product base class defines the interface , Specific products are needed to realize .

#include <iostream>
using namespace std;

//  Define product interface classes , food , Food can eat 
class Food {
public:
    virtual  void eat()=0;
};

//  Define specific products , Implementation interface , Fruit can be eaten 
class Fruit : public Food {
public:
    virtual void eat() {
        cout << "eat a fruit.\n";
    }
};

//  Vegetables can be eaten 
class Vegetable : public Food {
public:
    virtual void eat() {
        cout << "eat a vegetable.\n";
    }
};

//  Define factory class , You can create different types of products .
class FoodFactory {
public:
    Food* CreateFood(int type) {
        switch(type) {
            case 1:
                return new Fruit;
                break;
            case 2:
                return new Vegetable;
                break;
            default:
                return nullptr;
                break;
        }
    }
};

int main()
{
    FoodFactory * foodFactory = new FoodFactory;
    Food* fruit = foodFactory->CreateFood(1);
    Food* vegetable = foodFactory->CreateFood(2);
    fruit->eat();
    vegetable->eat();
    return 0;
}

advantage : Encapsulate complex creation logic , The caller does not need to know how to create an object , Make the original function or class more responsible , The code is simpler .

shortcoming : When new products are needed , Need to add a new product class , And modify the code of the factory class .


Two 、 Factory method model

Due to the disadvantages of the simple factory model , Developed a factory method model . The factory method pattern defines an interface ( Create products ), Multiple factories can implement this interface , But each factory only corresponds to one product . At this time, both factory and product classes have base classes , And all contain interfaces .

#include <iostream>
using namespace std;

class Food {
public:
    virtual  void eat()=0;
};

class Fruit : public Food {
public:
    virtual void eat() {
        cout << "eat a fruit.\n";
    }
};

class Vegetable : public Food {
public:
    virtual void eat() {
        cout << "eat a vegetable.\n";
    }
};

//  Define the factory interface ( Create products )
class Factory {
public:
    virtual Food* CreateFood()=0;
};

//  The fruit factory implements the interface , Create fruit . A factory can only create one product .
class FruitFactory : public Factory {
public:
    virtual Food* CreateFood() {
        return new Fruit;
    }
};

//  Vegetable factories create vegetables 
class VegetableFactory : public Factory {
public:
    virtual Food* CreateFood() {
        return new Vegetable;
    }
};


int main()
{
    Factory *fruitFactory = new FruitFactory;
    Factory *vegetableFactory = new VegetableFactory;
    Food *fruit = fruitFactory->CreateFood();
    Food* vegetable = vegetableFactory->CreateFood();
    fruit->eat();
    vegetable->eat();
    return 0;
}

advantage : Adding new factories is an expansion , It will not modify any codes of previous factory classes and product classes .

shortcoming : A factory class cannot create multiple products .


3、 ... and 、 Abstract factory pattern

Based on the factory method pattern , If a factory wants to produce more than one product , There is the abstract factory pattern . We can have a factory responsible for creating multiple different types of objects , This can effectively reduce the number of factory classes .

#include <iostream>
using namespace std;

//  Product 1 : food 
class Food {
public:
    virtual  void eat()=0;
};

//	 Fruit belongs to food 
class Fruit : public Food {
public:
    virtual void eat() {
        cout << "eat a fruit.\n";
    }
};

//	 Vegetables are food 
class Vegetable : public Food {
public:
    virtual void eat() {
        cout << "eat a vegetable.\n";
    }
};

//	 drinks 
class Drink {
public:
    virtual void drink() = 0;
};


//	 Watermelon Juice 
class XiGuaZhi : public Drink {
public:
    virtual void drink() {
        cout << "drink xiguazhi.\n";
    }
};

//	 Corn juice 
class YuMiZhi : public Drink {
public:
    virtual void drink() {
        cout << "drink yumizhi.\n";
    }
};

//	 Factory interface : Can produce food and drinks 
class Factory {
public:
    virtual Food* CreateFood()=0;
    virtual Drink* CreateDrink()=0;
};

//	 Fruit factory , Can produce fruit 、 Watermelon Juice 
class FruitFactory : public Factory {
public:
    virtual Food* CreateFood() {
        return new Fruit;
    }
    virtual Drink* CreateDrink() {
        return new XiGuaZhi;
    }
};

//	 Vegetable factory , Can produce vegetables 、 Corn juice 
class VegetableFactory : public Factory {
public:
    virtual Food* CreateFood() {
        return new Vegetable;
    }
    virtual Drink* CreateDrink() {
        return new YuMiZhi;
    }
};


int main()
{
    Factory *fruitFactory = new FruitFactory;
    Factory *vegetableFactory = new VegetableFactory;
    Food *fruit = fruitFactory->CreateFood();
    Food *vegetable = vegetableFactory->CreateFood();
    Drink *xiGuaZhi = fruitFactory->CreateDrink();
    Drink *yuMiZhi = vegetableFactory->CreateDrink();
    fruit->eat();
    vegetable->eat();
    xiGuaZhi->drink();
    yuMiZhi->drink();
    return 0;
}

advantage : A factory can produce multiple products

shortcoming : Few application scenarios

版权声明
本文为[baboon_ chen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210546351042.html