当前位置:网站首页>工厂方法模式
工厂方法模式
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;
}
边栏推荐
猜你喜欢

Technology sharing | Description of the electronic fence function in the integrated dispatching system

Openlayers 快速上手教程

自定义mvc框架复习

ssm访问数据库数据报错

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

JVM学习----垃圾回收调优

js秒表倒计时插件

unique in numpy & pandas

Openlayers Quick Start Tutorial

SQL Server 2019安装错误0x80004005 服务没有及时响应启动或控制请求详细解决方法
随机推荐
ssm访问数据库数据报错
Intouch Historian历史曲线配置导入导出
The ex-boyfriend bought chili water and threatened to rob his daughter. Can the woman apply for a personal safety protection order?
Technology sharing | Description of the electronic fence function in the integrated dispatching system
ABAP-OOAVL template program
liunx基础命令讲解
智能图像分析-智能家用电器图像目标检测统计计数检测与识别-艾科瑞特科技(iCREDIT)
网络流详解(流网图一般能够反映什么信息)
Solve the problem of Chinese garbled characters in exporting excel file names
DTG-SSOD: The latest semi-supervised detection framework, Dense Teacher (with paper download)
LeetCode_139_单词拆分
7种最常用数据分析思维,解决95%的分析难题
SQL Server如何建表
Chapter 11 Documents
FreeRTOS中名称规范
The use of QListView
There are several ways to jump to js source code, jump on the current page, jump on the blank page
Likou 58 - Left Rotation String
unique in numpy & pandas
Js scratchable latex style draw plug-in