Decorator pattern
I have learned several design patterns before, and today I will continue...
The decorator pattern belongs to the structural pattern, which is used to wrap and encapsulate the current class object. It is hoped that the function of the object can be expanded without modifying the current class object and class definition.
When called, the decorated object is used instead of the original object. , which provides additional functionality.
I don’t know if you have watched the video of handmade piano skewers [ https://www.bilibili.com/video/BV1334y1Z7kq?spm_id_from=333.999.0.0 ], it was originally a piano, but in order to play the piano, The kebabs, remodeled and decorated a bit, turned into special pianos that provided additional kebab functionality. Typical decorator pattern
: order to flexibly expand the functions of class objects.
Mainly include the following roles:
- Abstract component (
Component
): The abstraction of the original class being decorated, which can be an abstract class or an interface. - Concrete implementation class (
ConcreteComponent
): concrete abstracted class - Abstract Decorator (
Decorator
): Generic Abstractor - DETAILED decorator (
ConcreteDecorator
):Decorator
implementation class, in theory, eachConcreteDecorator
are extendedComponent
a functional object.
Advantages and disadvantages
advantage:
- Compared with class inheritance, wrapped objects are easier to expand and more flexible
- The decorator class and the decorated class are independent of each other, and the coupling degree is relatively low
- Fully adhere to the open-closed principle.
shortcoming:
- When the level of the wrapped object is deeper, it is more difficult to understand.
accomplish
First free a musical instrument interface class Instrument
:
public interface Instrument {
void play();
}
Get two instruments Piano
and Guitar
to implement the instrument interface:
public class Piano implements Instrument{
@Override
public void play() {
System.out.println("手工耿弹奏钢琴");
}
}
public class Guitar implements Instrument{
@Override
public void play() {
System.out.println("手工耿弹吉他");
}
}
No matter if you want to grill while playing guitar, grill while playing piano, or take a bath while playing piano, no matter what your needs are, we abstract a decorator class to package and decorate musical instruments.
public class InstrumentDecorator implements Instrument{
protected Instrument instrument;
public InstrumentDecorator(Instrument instrument) {
this.instrument = instrument;
}
@Override
public void play() {
instrument.play();
}
}
The above is an abstract decoration class. We have to do some actual actions, so we need to do a barbecue function.
public class BarbecueInstrumentDecorator extends InstrumentDecorator {
public BarbecueInstrumentDecorator(Instrument instrument) {
super(instrument);
}
@Override
public void play() {
instrument.play();
barbecue();
}
public void barbecue(){
System.out.println("手工耿在烧烤");
}
}
have a test:
public class DecoratorDemo {
public static void main(String[] args) {
Instrument instrument = new Piano();
instrument.play();
System.out.println("----------------------------------------");
InstrumentDecorator barbecuePiano = new BarbecueInstrumentDecorator(new Piano());
barbecuePiano.play();
System.out.println("----------------------------------------");
InstrumentDecorator barbecueGuitar = new BarbecueInstrumentDecorator(new Guitar());
barbecueGuitar.play();
}
}
The test results are as follows. It can be seen that when there is no decoration, you can only do one thing. After the decoration, the object can play a musical instrument or barbecue. I can't help but sigh: It turns out that handmade Geng is a master of design mode:
手工耿弹奏钢琴
----------------------------------------
手工耿弹奏钢琴
手工耿在烧烤
----------------------------------------
手工耿弹吉他
手工耿在烧烤
Summary
Design patterns are not a silver bullet, but a better practice that has evolved in software engineering or programming. We can't design patterns for the sake of designing patterns, learning theory is just to use it better, to know when to use it and when not to use it.
The decorator mode is designed to expand its functions without destroying the original structure. In fact, it is widely used Java IO
DataInputStream dis = new DataInputStream(
new BufferedInputStream(
new FileInputStream("test.txt")
)
);
First pass BufferedInputStream
FileInputStream
to make a decoration object, then pass the decoration object to DataInputStream
, and decorate it again. In the end, FileInputStream
was packaged into DataInputStream
, and interested students can look at the source code.
【About the author】 :
Qin Huai, [ 161de21123975b Qin Huai Grocery Store ], the road to technology is not at one time, the mountains are high and the rivers are long, even if it is slow, it is endless.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。