当前位置:网站首页>"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 (¯﹃¯)
边栏推荐
- I2C设备驱动程序的层次结构
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
- Gonzalez Digital Image Processing Chapter 1 Introduction
- easygui使用的语法总结
- D2. Chopping Carrots (Hard Version) (每日一题)
- (原创)自定义一个滚屏的RecyclerView
- 006操作符简介
- Quickly set up dobbo demo
- Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
- C language and SQL Server database technology
猜你喜欢

阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
![Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]

D2. picking carrots (hard version) (one question per day)

45padding不会撑开盒子的情况

45padding won't open the box

32 chrome调试工具的使用

微信公众号正式环境上线部署,第三方公众平台接入

The concept and operation rules of calculus of variations

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

Filters get the data in data; Filters use data in data
随机推荐
Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
41 picture background synthesis - colorful navigation map
C language and SQL Server database technology
easygui使用的语法总结
Jqgrid select all cancel single line click cancel event
Ssh server rejected password
物理量与单位符号的书写标准
[C题目]牛客 链表中倒数第k个结点
云安全技术发展综述
Copy files / folders through Robocopy
各种平台dpkg包下载地址(包括arm64)
Is it safe for Guolian securities to buy shares and open an account?
Educational Codeforces Round 132 (Rated for Div. 2) C,D+AC自动机
Add the jar package under lib directory to the project in idea
Content type corresponding to office file
GameFramework制作游戏(二)制作UI界面
[MySQL must know and know] trigger | permission management
The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%
Ten common application scenarios of redis
C language and SQL Server database technology