Article directory
What is the decorator pattern
Now there is a piece of cake, if you add cream, it will become a cream cake, if you add strawberries, it will become a strawberry cream cake, and if you add candles, it will become a birthday cake.
Objects in programs are very similar to cakes. Comparing an object to a cake, and adding functions to modify the object, it becomes a more definite object. Design patterns that continuously add decorations to objects like this are called (Decorator) decorator patterns
example:
Extend the class that originally only displayed numbers to a class that displays addition and subtraction operations and displays the results
Top-level interface:
NumberDisplay
package StructuralPattern.DecoratorMode;
/**
* 顶层数字显示接口
*/
public interface NumberDisplay {
void setTxt(int num);
String getTxt();
}
Core implementation class: display digital
NumberShow
package StructuralPattern.DecoratorMode;
/**
* 数字显示实现类
*/
public class NumberShow implements NumberDisplay{
private int num;
@Override
public void setTxt(int num) {
this.num = num;
}
@Override
public String getTxt() {
return String.valueOf(num);
}
}
Extended decorator abstract class, specially used for extended abstract class
ArithmeticDecorator
package StructuralPattern.DecoratorMode;
/**
* 运算装饰器
*/
public abstract class ArithmeticDecorator implements NumberDisplay {
protected NumberDisplay target;
public ArithmeticDecorator(NumberDisplay target) {
this.target = target;
}
public void setTxt(int num) {
this.target.setTxt(num);
}
}
Plus Decorator
PlusDecorate
package StructuralPattern.DecoratorMode;
/**
* 加号装饰器
*/
public class PlusDecorate extends ArithmeticDecorator {
private int num;
public PlusDecorate(NumberDisplay target,int num){
super(target);
this.num = num;
}
@Override
public String getTxt() {
return target.getTxt() + " + " + num;
}
}
Minus decorator
MinusDecorate
package StructuralPattern.DecoratorMode;
/**
* 减号装饰
*/
public class MinusDecorate extends ArithmeticDecorator {
private int num;
public MinusDecorate(NumberDisplay target,int num) {
super(target);
this.num = num;
}
@Override
public String getTxt() {
return target.getTxt() + " - " + num;
}
}
The equal sign decorator
EquelDecorate
package StructuralPattern.DecoratorMode;
/**
* 等号装饰
*/
public class EquelDecorate extends ArithmeticDecorator {
private int num;
public EquelDecorate(NumberDisplay target, int num) {
super(target);
this.num = num;
}
@Override
public String getTxt() {
return target.getTxt() + " = " + num;
}
}
Main
package StructuralPattern.DecoratorMode;
/**
* Main
*/
public class Main {
public static void main(String[] args) {
// 只展示核心功能
NumberDisplay display1 = new NumberShow();
display1.setTxt(1);
System.out.println(display1.getTxt());
// 展示扩展功能
NumberDisplay display2 = new PlusDecorate(display1,1);
NumberDisplay display3 = new MinusDecorate(display2,2);
NumberDisplay display4 = new EquelDecorate(display3,0);
System.out.println(display4.getTxt());
// 展示扩展功能
NumberDisplay display = new EquelDecorate(new MinusDecorate(new PlusDecorate(new NumberShow(),2),4),0);
display.setTxt(2);
System.out.println(display.getTxt());
}
}
result
Summarize
From the above example, we can see that the number display of the core class NumberShow has not changed. Although the function has been extended many times, the core interface api can still be used. The main purpose of the Decorator mode is to increase the function of the object by adding decorations. If it is necessary to add core functions, add methods to the top-level interface and increase the functions of the top-level interface implementation class; for additional interfaces, it is extended through the abstract decorator class.