当前位置:网站首页>"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 (¯﹃¯)
边栏推荐
- IP address classification, which determines whether a network segment is a subnet supernetwork
- 关于ROS2安装connext RMW的进度条卡在13%问题的解决办法
- 45padding won't open the box
- 51单片机学习笔记(2)
- Quickly set up dobbo demo
- PHP 通过原生CURL实现非阻塞(并发)请求模式
- C language and SQL Server database technology
- Thymeleaf setting disabled
- Kibana operation es
- 云安全技术发展综述
猜你喜欢

河源市区推出消防安全主题奶茶 助推夏季火灾防控
![[MySQL must know and know] trigger | permission management](/img/59/cb805d972097a6a8ed7f3ae454a91d.png)
[MySQL must know and know] trigger | permission management

Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer

Filters get the data in data; Filters use data in data

Overview of cloud security technology development

I2C设备驱动程序的层次结构
![应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]

39 simple version of millet sidebar exercise

Realsense ROS installation configuration introduction and problem solving

45padding won't open the box
随机推荐
LeetCode_ String_ Medium_ 151. Reverse the words in the string
gson与fastjson
变分(Calculus of variations)的概念及运算规则
Dpkg package download addresses of various platforms (including arm64)
Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
MySQL的登陆【数据库系统】
Gateway reports an error service_ UNAVAILABLE
LeetCode_字符串_中等_151.颠倒字符串中的单词
Software testing -- 1. Outline of software testing knowledge
[Nuxt 3] (十一) 传送 & 模块
How to use the random number function of JMeter
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
国联证券买股票开户安全吗?
直播课堂系统05-后台管理系统
Realsense ROS installation configuration introduction and problem solving
35 快速格式化代码
(原创)自定义一个滚屏的RecyclerView
51单片机学习笔记(1)
35 quick format code
OverTheWire-Natas