当前位置:网站首页>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語言開發天天生鮮項目第三天 案例-新聞發布系統二
- Bracket matching -- [implementation of one-dimensional array]
- LeetCode 74、搜索二维矩阵
- 笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
- 中创存储|想要一个好用的分布式存储云盘,到底该怎么选
- C# 知识
- The problem of 1 pixel border on the mobile terminal
- BMP JPEG picture to vector image contourtrace
- Introduction to standardization, regularization and normalization
- UnhandledPromiseRejectionwarning:CastError: Cast to ObjectId failed for value
猜你喜欢

2022dasctf APR x fat epidemic prevention challenge crypto easy_ real

LeetCode 116. 填充每个节点的下一个右侧节点指针

Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high

GO語言開發天天生鮮項目第三天 案例-新聞發布系統二

Linux中,MySQL的常用命令

LeetCode 74、搜索二维矩阵

Elastic box model

Flex layout

又一款数据分析神器:Polars 真的很强大

Common commands of MySQL in Linux
随机推荐
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
Go language development Daily Fresh Project Day 3 Case - Press Release System II
How to configure SSH public key in code cloud
Is qiniu school useful and is the recommended securities account safe
Leetcode 232, queue with stack
How many hacking methods do you know?
go map
Use of node template engine
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
[stack and queue topics] - sliding window
Learn to C language fourth day
浅谈数据库设计之三大范式
Go limit depth traversal of files in directory
Common commands of MySQL in Linux
XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
Awk example skills
Parsing methods of JSON data in C - jar and jobobject: error reading jar from jsonreader Current JsonReader item
Commande dos pour la pénétration de l'Intranet
Vulnhub DC: 1 penetration notes
Send email to laravel