当前位置:网站首页>"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 (¯﹃¯)
边栏推荐
- Is it safe for Guolian securities to buy shares and open an account?
- Log4j2 basic configuration
- Melodic + Realsense D435i 配置及错误问题解决
- [C题目]力扣876. 链表的中间结点
- 43 box model
- English语法_不定代词 - other / another
- 51单片机学习笔记(1)
- IP address classification, which determines whether a network segment is a subnet supernetwork
- 32 chrome调试工具的使用
- Paddlenlp之UIE关系抽取模型【高管关系抽取为例】
猜你喜欢
随机推荐
Throwing OutOfMemoryError “Could not allocate JNI Env“
LeetCode_因式分解_简单_263.丑数
[MySQL must know and know] trigger | permission management
Jqgrid select all cancel single line click cancel event
【MySQL系列】-索引知多少
Paddlenlp's UIE relationship extraction model [executive relationship extraction as an example]
Under the epidemic, the biomedical industry may usher in breakthrough development
Awk from getting started to digging in (20) awk parsing command line parameters
各种平台dpkg包下载地址(包括arm64)
Dpkg package download addresses of various platforms (including arm64)
国联证券买股票开户安全吗?
BigDecimal rounds the data
Gameframework making games (II) making UI interface
About RDBMS and non RDBMS [database system]
Overview of cloud security technology development
Gonzalez Digital Image Processing Chapter 1 Introduction
Add the jar package under lib directory to the project in idea
PS making and loading GIF pictures tutorial
Awk from getting started to digging in (21) awk script debugging
物理量与单位符号的书写标准








![[MySQL must know and know] trigger | permission management](/img/59/cb805d972097a6a8ed7f3ae454a91d.png)