当前位置:网站首页>Factory mode
Factory mode
2022-04-23 20:46:00 【baboon_ chen】
List of articles
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
边栏推荐
- go interface
- The problem of 1 pixel border on the mobile terminal
- 3-5通过XSS获取cookie以及XSS后台管理系统的使用
- Come in and teach you how to solve the problem of port occupation
- LeetCode 116. Populate the next right node pointer for each node
- [stack and queue topics] - sliding window
- 内网渗透之DOS命令
- 缓存淘汰算法初步认识(LRU和LFU)
- Another data analysis artifact: Polaris is really powerful
- How to configure SSH public key in code cloud
猜你喜欢
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
GSI-ECM工程建设管理数字化平台
Syntax Error: TypeError: this. getOptions is not a function
Addition, deletion, modification and query of MySQL advanced table
Elastic box model
[SQL] string series 2: split a string into multiple lines according to specific characters
LeetCode 116. Populate the next right node pointer for each node
Common problems in deploying projects with laravel and composer for PHP
UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
随机推荐
2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
How many hacking methods do you know?
Resolve the eslint warning -- ignore the warning that there is no space between the method name and ()
Thirty What are VM and VC?
MySQL stored procedures and functions
电脑越用越慢怎么办?文件误删除恢复方法
"Meta function" of tidb 6.0: what is placement rules in SQL?
2022dasctf APR x fat epidemic prevention challenge crypto easy_ real
Go限制深度遍历目录下文件
go interface
Introduction to intrusion detection data set
LeetCode 1351、统计有序矩阵中的负数
Solution: NPM err! code ELIFECYCLE npm ERR! errno 1
laravel 发送邮件
內網滲透之DOS命令
Latex formula
6-5 string - 2 String copy (assignment) (10 points) the C language standard function library includes the strcpy function for string copy (assignment). As an exercise, we write a function with the sam
Leetcode 994, rotten orange
SQL gets the latest record of the data table
又一款数据分析神器:Polars 真的很强大