当前位置:网站首页>"How to use" decorator mode
"How to use" decorator mode
2022-07-25 14:53:00 【Senior fishing Engineer】
The best way to eliminate fear is to face it directly , cheer up :)
This paper
Using inheritance to design the behavior of subclasses , Statically determined at compile time , And all subclasses will inherit this behavior . If you use composition to extend the behavior of objects , It can be dynamically extended at runtime .
Decorator mode : Attach responsibility to the object dynamically . To extend functionality , Decorators offer a more flexible alternative to inheritance .
OO principle : Open to expansion , Turn off for changes .
principle

- Component It's the father of the decorated object , It's an interface , Define the behavior of the adorned
- Decorator It's the decorator's father , It inherited Component, It ensures that the types of decorators and decoratees are consistent
- Decorator My sons all have one Component References to objects , Used to correct Compoment Behavior doing “ decorate ”
Realization
public interface Component {
String DESCRIPTION = " I am a component";
default String getDescription(){
return DESCRIPTION;
}
/** * Decorated with extended behavior */
String link();
}
public interface Decorator extends Component {
/** * Decorator overrides this method to add his own attribute definition , Or extend this method * @return */
@Override
String getDescription();
}
public class ConcreteComponentB implements Component {
@Override
public String link() {
return " I am a Component My second son is right link The implementation of the -->";
}
}
public class ConcreteDecoratorB implements Decorator {
private Component component;
public ConcreteDecoratorB(Component component) {
this.component = component;
}
@Override
public String getDescription() {
return component.getDescription() + "--> I am the packer B Description of ";
}
@Override
public String link() {
return "Decorator My second daughter is right Component Of link Made a decoration -->" + component.link();
}
}
Test class
public class TestDrive {
public static void main(String[] args) {
Component componentB = new ConcreteComponentB();
componentB = new ConcreteDecoratorA(componentB);
componentB = new ConcreteDecoratorB(componentB);
System.out.println("componentB.getDescription() = " + componentB.getDescription());
System.out.println("componentB.link() = " + componentB.link());
}
}
Output
componentB.getDescription() = I am a component--> I am the packer A Description of --> I am the packer B Description of
componentB.link() = Decorator My second daughter is right Component Of link Made a decoration -->
Decorator My eldest daughter is right Component Of link Made a decoration --> I am a Component My second son is right link The implementation of the -->
summary
- Decorator mode uses a noun called “ Combine ”, In fact, its soul is “ polymorphic ”, See clearly , No “ metamorphosis ” :)
- In fact, the idea of usage is very simple , To get a “ Decorator ” Interface inheritance “ Decorated ” also @Override to want to “ decorate ” Methods 【 Not all methods , Otherwise, this is not decoration, but demolition 】, Then different decorator implementation classes decorate this method . The premise of decoration is to hold a quotation of the decorator , That's the only way , To the method that has been realized in the decorator “ Move one's hand or one's foot ”【 Everyone is in your room , You can do whatever you want :)】
Q&A
Q: So much Component and Decorator Implementation class of , Isn't it life “ Class blast ”?
A: Engage in “ Factory mode ” Well
PS : Xiba , Another boring day (¯﹃¯)
边栏推荐
- The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%
- L1和L2正则化
- 41 picture background synthesis - colorful navigation map
- Software testing -- 1. Outline of software testing knowledge
- Writing standard of physical quantities and unit symbols
- Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
- easygui使用的语法总结
- [eloquence] negotiation persuasion skills and Strategies
- BIO、NIO、AIO示例
- Go语言创始人从Google离职
猜你喜欢

Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount

Melodic + Realsense D435i 配置及错误问题解决

27 classification of selectors

PS making and loading GIF pictures tutorial

变分(Calculus of variations)的概念及运算规则

As methods for viewing and excluding dependencies

sudo rosdep init Error ROS安装问题解决方案

Resource not found: rgbd_launch 解决方案

32 use of chrome debugging tools

32 chrome调试工具的使用
随机推荐
(original) customize a scrolling recyclerview
LeetCode-198-打家劫舍
kibana操作es
BigDecimal rounds the data
006操作符简介
Vs2017 large factory ERP management system source code factory general ERP source code
As methods for viewing and excluding dependencies
冈萨雷斯 数字图像处理 第一章绪论
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]
Qt connect 中, SIGNAL,SLOT 与 lambda 对比
41 图片背景综合-五彩导航图
SSH服务器拒绝了密码
English grammar_ Indefinite pronoun - other / other
[MySQL series] - how much do you know about the index
【口才】谈判说服技巧及策略
[C topic] force buckle 876. Intermediate node of linked list
基于AMD EPYC服务器的EDA芯片设计解决方案
H5页面input输入框弹起数字键盘,需要支持小数点
关于RDBMS和非RDBMS【数据库系统】
easygui使用的语法总结