当前位置:网站首页>工厂方法模式
工厂方法模式
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;
}
边栏推荐
- Free Chinese-English Translation Software - Automatic Batch Chinese-English Translation Software Recommended Daquan
- LeetCode_377_组合总和Ⅳ
- SuperSlide系列之轮播图
- 【第六届强网杯CTF-Wp】
- Openlayers 快速上手教程
- FreeRTOS creation tasks - dynamic creation, static creation
- js九宫格样式抽奖插件
- Likou 704 - binary search
- Object.entries()
- Seneor Exposure Basics
猜你喜欢

js stopwatch countdown plugin

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

自定义mvc框架复习

Import and export data of SQL Server database

手撸架构,网络 面试36问

Four seasons of trees realized by svg

Openlayers Quick Start Tutorial

To eliminate air bubbles to save the mushroom h5 small game source code

pig4cloud服务架构使用

手撸架构,Mysql 面试126问
随机推荐
FreeRTOS--stack experiment
Chapter 14 Manually create a REST service (2)
Solve the problem of Chinese garbled characters in exporting excel file names
Chapter 11 Documents
pig4cloud服务架构使用
数据湖(二):什么是Hudi
Name conventions in FreeRTOS
Distributed current limiting, hand & redisson implementation
np.nan, np.isnan, None, pd.isnull, pd.isna finishing and summary
Data Lake (3): Hudi Concept Terminology
WebUI自动化测试框架搭建从0到1(完整源码)更新完毕
FreeRTOS experiment--one function creates multiple tasks
Four seasons of trees realized by svg
A powerful js pop-up alert plugin
Openlayers 快速上手教程
kvm部署
SQL中字符串拼接
智能图像分析-智能家用电器图像目标检测统计计数检测与识别-艾科瑞特科技(iCREDIT)
js炫酷仪表盘插件
After Effects 教程,如何在 After Effects 中对蒙版进行动画绘制?