当前位置:网站首页>Decorator mode of structural mode
Decorator mode of structural mode
2022-06-22 19:34:00 【OldZhangYH】
Decorator pattern of structural pattern
Source code
Decorator mode
Decorator mode can be used without changing the existing object structure , Dynamically add some responsibilities to the object ( Just add some extra functions ).
structure
- Abstract component class (Component): Define an abstract class or interface to specify the object that needs additional functions .
- Concrete component class (ConcreteComponent): Implement abstract components , Add some responsibilities to the character by decorating it .
- Abstract decorator (Decorator): Inherit or implement abstract interfaces , And contains instances of specific components , You can extend the function of a specific component through its subclasses .
- Concrete decoration (ConcreteDecorator): Concrete methods for implementing abstract decoration classes , And add responsibilities to specific component classes .
Case study
Order at a fast food restaurant , We can order a fried rice , Then add an egg , Add a beef . Ordering a fried rice is a concrete construction class , Add an egg , Adding beef is an extra duty .
Of course, we can also use inheritance , Let an egg fried rice inherit fried rice , Beef fried rice inherits fried rice . But there may be too many such classes , And the scalability is not good .
The class diagram is as follows , The key point is ExtFood abstract class , He inherited FastFood Also cited FastFood.
public class test {
public static void main(String[] args) {
Rice rice=new Rice();
System.out.println(rice.getName()+":" +rice.cost());
Egg egg=new Egg(rice);
System.out.println(egg.getName()+":" +egg.cost());
Beef beef=new Beef(egg);
System.out.println(beef.getName()+":" +beef.cost());
}
}
Output :
Fried rice :10.0
Fried rice with eggs :11.0
Fried rice with egg and beef :16.0
advantage
- Decorator pattern can bring more flexible extension functions than inheritance , It's more convenient to use . By combining different specific decoration classes, we can obtain diversified results with different functions . The principle of opening and closing is obeyed . Inheritance is a static additional responsibility ( It's dead ), Decorator is a dynamic additional responsibility .
- Concrete decoration classes and concrete component classes can be extended independently of each other without coupling .( Ham sausage can be added 、 Fried noodles or something ). Decorator pattern can dynamically extend the functions of implementation classes .
Use scenarios
- When it is not possible or convenient to use inheritance to extend the system .
- For example, there are a large number of independent extensions in the system , In this way, in order to support their group, there will be many subclasses .
- final class
- Without affecting other objects , Dynamic 、 Add responsibilities to individual objects transparently .
- When the function of an object needs to be added and removed dynamically .( The eggs are sold out , Just delete the eggs )
JDK The case in
IO The wrapper class in the stream uses the decorator pattern .BufferedXXXXX.BufferedWriter The class diagram , The key on the right is both inheritance and aggregation .
The difference between and agent mode
The same thing
- To implement the same target object as the target class .
- Declare the target object in the class .
- You can extend the target method without modifying the target class .
Difference
- The decorator is to enhance the target object , Proxy mode is to protect and hide objects
- The target object declared in the decorator is transmitted from the outside , The proxy pattern is created in the class .
边栏推荐
- 给对象赋值
- mini-Web框架:模板替换与路由列表功能开发 | 黑马程序员
- Intelligent procurement system solution for processing and manufacturing industry: help enterprises realize integrated and Collaborative Procurement in the whole process
- Pat a 1093 count Pat's (25 points)
- shell脚本详解(十)——sed编辑器的使用方法
- Message Oriented Middleware (I) MQ explanation and comparison of four MQS
- Flutter series -dart basic grammar learning
- 贪心之分配问题(2)
- wpa_ State machine migration of supplicant
- canvas给图片画框框
猜你喜欢

ActiveReports报表实战应用教程(十九)——多数据源绑定

实现领域驱动设计 - 使用ABP框架 - 解决方案概览

shell脚本详解(二)——条件测试、if语句和case分支语句

5G 短消息解决方案

shell脚本(五)——函数

STM32 control matrix key, Hal library, cubemx configuration

Common technical notes

Digital supply chain centralized purchase platform solution for mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase

20billion vs 5billion, how much is the "dehydration" little red book worth?

Problems of different renderers running on the web with flutter2.0
随机推荐
Pat a 1093 count Pat's (25 points)
有效的括号
Intelligent procurement system solution for processing and manufacturing industry: help enterprises realize integrated and Collaborative Procurement in the whole process
修改antd tree组件,使其子类横向排列。
wpa_ CLI parameter description
5G 短消息解决方案
shell脚本详解(二)——条件测试、if语句和case分支语句
新唐NUC980使用记录:开发环境准备与编译配置基础说明
函数的导数与微分的关系
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
如何提高工作效率?苹果电脑效率工具合集
Digital commerce cloud: analyze the design idea of B2B2C multi-user mall system architecture, and open a new era of intelligent mall
Chrome suddenly can't copy and paste
shell脚本(五)——函数
std::enable_ shared_ from_ This error: error: expected template name before '<' token
IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展
Experiment 7 trigger
Error in created hook: “TypeError: Cannot read property ‘tableId‘ of undefined“
[suggestions collection] common usage scenarios of message queue
IPLOOK和思博伦通信建立长期合作