当前位置:网站首页>"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 (¯﹃¯)
边栏推荐
- SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你
- I2C device driver hierarchy
- Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
- Content type corresponding to office file
- Gonzalez Digital Image Processing Chapter 1 Introduction
- 河源市区推出消防安全主题奶茶 助推夏季火灾防控
- Vs2017 large factory ERP management system source code factory general ERP source code
- 【口才】谈判说服技巧及策略
- 云安全技术发展综述
- Go language founder leaves Google
猜你喜欢

牛客多校 E G J L

The security market has entered a trillion era, and the security B2B online mall platform has been accurately connected to deepen the enterprise development path

51单片机学习笔记(2)

45padding won't open the box

Sudo rosdep init error ROS installation problem solution

Resource not found: rgbd_ Launch solution
![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]

The concept and operation rules of calculus of variations

Go language founder leaves Google

Go语言创始人从Google离职
随机推荐
L1和L2正则化
43 box model
各种平台dpkg包下载地址(包括arm64)
35 快速格式化代码
32 chrome调试工具的使用
Dpkg package download addresses of various platforms (including arm64)
GameFramework制作游戏(一)
(original) customize a scrolling recyclerview
Alibaba cloud installs mysql5.7
GameFramework制作游戏(二)制作UI界面
基于浏览器的分屏阅读
Content type corresponding to office file
MySQL sort
SSM framework integration, simple case
Thymeleaf notes
How to use the random number function of JMeter
Idea error failed to determine a suitable driver class
Practical guide for network security emergency response technology (Qianxin)
IP address classification, which determines whether a network segment is a subnet supernetwork
Log4j2 basic configuration