当前位置:网站首页>工厂方法模式
工厂方法模式
2022-08-02 12:33:00 【l_ethan】
简介:工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不在负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。
工厂方法模式是简单工厂模式的衍生,解决了许多简单工厂模式的问题。首先完全实现’开-闭‘原则,实现了可拓展。

优点:
1.不需要记住具体类名
2.实现了对象创建和使用的分离
3.系统的可扩展性也就变地非常好无需修改接口和原类。
缺点:
1.增加系统中类的个数,复杂度和理解度增加
2.增加了系统的抽象性和理解难度
案例:
#include <iostream>
using namespace std;
class AbstractFruit {
public:
virtual void show() = 0;
};
class Apple :public AbstractFruit {
public:
virtual void show() {
cout << "apple" << endl;
}
};
class Banana :public AbstractFruit {
public:
virtual void show() {
cout << "banana" << endl;
}
};
class AbstractFruitFactor {
public:
virtual AbstractFruit* createfruit()=0;
};
class AppleFactor :public AbstractFruitFactor {
public:
virtual AbstractFruit* createfruit() {
return new Apple;
}
};
class BananaFactor :public AbstractFruitFactor {
public:
virtual AbstractFruit* createfruit() {
return new Banana;
}
};
int main()
{
AbstractFruitFactor* af = nullptr;
AbstractFruit* f = nullptr;
af = new AppleFactor;
f = af->createfruit();
f->show();
delete af;
delete f;
return 0;
}
边栏推荐
猜你喜欢

np.nan, np.isnan, None, pd.isnull, pd.isna 整理与小结

js cool dashboard plugin

DTG-SSOD: The latest semi-supervised detection framework, Dense Teacher (with paper download)

liunx基础命令讲解

Likou 704 - binary search

MD5 detailed explanation (check file integrity)

Speed up your programs with bitwise operations

Likou 209 - String with the Minimum Length - Sliding Window Method

Likou 977-Squaring of ordered arrays - brute force method & double pointer method

SQL Server 数据库之生成与执行 SQL 脚本
随机推荐
zabbix自动化监控脚本
Drools(8): WorkBench uses
js cool dashboard plugin
【第六届强网杯CTF-Wp】
Openlayers Quick Start Tutorial
Interpretation of new features | MySQL 8.0 GIPK invisible primary key
Hand rolled architecture, 41 Redis interview asked
The use of QListView
网络流详解(流网图一般能够反映什么信息)
手撸架构,Mysql 面试126问
第十四章 手动创建 REST 服务(二)
SQL Server 2014 installation tutorial (nanny-level graphic tutorial)
LeetCode_377_组合总和Ⅳ
openGauss数据库基本操作(超详细)
FreeRTOS--优先级实验
Name conventions in FreeRTOS
Do you really understand the business process service BPass?
To eliminate air bubbles to save the mushroom h5 small game source code
MFC入门教程(深入浅出MFC)
ABAP-OOAVL template program