当前位置:网站首页>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
边栏推荐
- Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
- go slice
- [PTA] get rid of singles
- Addition, deletion, modification and query of MySQL advanced table
- LeetCode 709、转换成小写字母
- Another data analysis artifact: Polaris is really powerful
- JSX syntax rules
- 41. 缺失的第一个正数
- The problem of 1 pixel border on the mobile terminal
- Solve the Chinese garbled code of URL in JS - decoding
猜你喜欢
Gsi-ecm digital platform for engineering construction management
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Common problems in deploying projects with laravel and composer for PHP
go defer
LeetCode 116. 填充每个节点的下一个右侧节点指针
GSI-ECM工程建设管理数字化平台
DOS command of Intranet penetration
[SQL] string series 2: split a string into multiple lines according to specific characters
Flex layout
MySQL基础合集
随机推荐
Summary and effect analysis of methods for calculating binocular parallax
GO語言開發天天生鮮項目第三天 案例-新聞發布系統二
LeetCode 232、用栈实现队列
Psychological formula for converting RGB to gray value
On the three paradigms of database design
On IRP from the perspective of source code
Is qiniu school useful and is the recommended securities account safe
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
Queue template code
Unity asset import settings
黑客的入侵方式你知道几种?
MySQL basic collection
UKFslam
I JS deep copy and shallow copy
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
Linux中,MySQL的常用命令
[PTA] get rid of singles
Leetcode 709, convert to lowercase
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
go defer