当前位置:网站首页>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
边栏推荐
- LeetCode 74、搜索二维矩阵
- An error occurs when the addressable assets system project is packaged. Runtimedata is null
- [PTA] l1-006 continuity factor
- Async function ------ ES6
- 6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
- JSX syntax rules
- C migration project record: modify namespace and folder name
- On IRP from the perspective of source code
- SQL: query duplicate data and delete duplicate data
- Introduction to intrusion detection data set
猜你喜欢
浅谈数据库设计之三大范式
Latex formula
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
LeetCode 116. 填充每个节点的下一个右侧节点指针
Mysql database common sense storage engine
LeetCode 994、腐烂的橘子
GSI-ECM工程建设管理数字化平台
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
LeetCode 74、搜索二维矩阵
随机推荐
Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
LeetCode 994、腐烂的橘子
LeetCode-279-完全平方数
A login and exit component based on token
Vulnhub DC: 1 penetration notes
Selenium displays webdriverwait
LeetCode 116. 填充每个节点的下一个右侧节点指针
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
On the three paradigms of database design
Leetcode 20. Valid parentheses
Unity asset import settings
Pikachuxss how to get cookie shooting range, always fail to return to the home page
Elastic box model
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
go map
LeetCode 232、用栈实现队列
Preliminary understanding of cache elimination algorithm (LRU and LFU)
On IRP from the perspective of source code
UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
How to use PM2 management application? Come in and see