Design Patterns(七) Decorator

Decorator pattern allows a user to add new functionality to an existing object without altering its structure. This type of design pattern comes under structural pattern as this pattern acts as a wrapper to existing class.

前言

    装饰者模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

装饰者模式

装饰者模式原理:

1) 装饰者模式就像 打包一个快递:
  主体:比如:陶瓷、衣服 (Component) // 被装饰者;
  包装:比如:报纸填充、塑料泡沫、纸板、木板(Decorator)。
2) Component:
  主体:比如类似前面的Drink。
3) ConcreteComponent和Decorator:
  ConcreteComponent:具体的主体,比如前面的各个单品咖啡;
  Decorator: 装饰者,比如各调料。
4) 在Component 与ConcreteComponent 之间,如果ConcreteComponent类很多,还可以设计一个缓冲层,将共有的部分提取出来,抽象层一个类。




装饰者模式-UML图:





装饰者模式-案例图:



代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @Auther: Arsenal
* @Date: 2020-03-14 17:32
* @Description: 装饰者模式
*/
public class Decorator {
public static void main(String[] args) {

/*System.out.println("==========Bayonet==========");
Weapon weapon1 = new Bayonet(new Pistol());
weapon1.aimAndFire();
Weapon weapon2 = new Bayonet(new AK47());
weapon2.aimAndFire();
Weapon weapon3 = new Bayonet(new AWP());
weapon3.aimAndFire();
System.out.println("==========Sniperscope==========");
Weapon weapon4 = new Sniperscope(new Pistol());
weapon4.aimAndFire();
Weapon weapon5 = new Sniperscope(new AK47());
weapon5.aimAndFire();
Weapon weapon6 = new Sniperscope(new AWP());
weapon6.aimAndFire();
System.out.println("==========Cartridge==========");
Weapon weapon7 = new Cartridge(new Pistol());
weapon7.aimAndFire();
Weapon weapon8 = new Cartridge(new AK47());
weapon8.aimAndFire();
Weapon weapon9 = new Cartridge(new AWP());
weapon9.aimAndFire();*/


System.out.println("====================Pistol====================");
Weapon weapon1 = new Pistol();
weapon1 = new Bayonet(weapon1);
weapon1 = new Sniperscope(weapon1);
weapon1 = new Cartridge(weapon1);
weapon1.aimAndFire();
System.out.println("====================AK47====================");
Weapon weapon2 = new AK47();
weapon2 = new Cartridge(weapon2);
weapon2 = new Bayonet(weapon2);
weapon2 = new Sniperscope(weapon2);
weapon2.aimAndFire();
System.out.println("====================AWP====================");
Weapon weapon3 = new AWP();
weapon3 = new Sniperscope(weapon3);
weapon3 = new Cartridge(weapon3);
weapon3 = new Bayonet(weapon3);
weapon3.aimAndFire();
}
}

/**
* 被装饰者
*/
interface Weapon {
void aimAndFire();
}

/**
* base weapon
*/
class Pistol implements Weapon {

@Override
public void aimAndFire() {
System.out.println("手枪瞄准射击");
}
}

/**
* base weapon
*/
class AK47 implements Weapon {

@Override
public void aimAndFire() {
System.out.println("冲锋枪瞄准射击");
}
}

/**
* base weapon
*/
class AWP implements Weapon {

@Override
public void aimAndFire() {
System.out.println("狙击枪瞄准射击");
}
}

/**
* 装饰者
*/
abstract class WeaponDecorator implements Weapon {

Weapon weapon;

WeaponDecorator(Weapon weapon) {
this.weapon = weapon;
}

@Override
public void aimAndFire() {
weapon.aimAndFire();
}
}

/**
* weapon accessory
*/
class Bayonet extends WeaponDecorator {

Bayonet(Weapon weapon) {
super(weapon);
}

@Override
public void aimAndFire() {
addBayonet();
super.aimAndFire();
}

void addBayonet() {
System.out.print("加刺刀的-");
}
}

/**
* weapon accessory
*/
class Sniperscope extends WeaponDecorator {

Sniperscope(Weapon weapon) {
super(weapon);
}

@Override
public void aimAndFire() {
addSniperscope();
super.aimAndFire();
}

void addSniperscope() {
System.out.print("加瞄准镜的-");
}
}

/**
* weapon accessory
*/
class Cartridge extends WeaponDecorator {

Cartridge(Weapon weapon) {
super(weapon);
}

@Override
public void aimAndFire() {
addCartridge();
super.aimAndFire();
}

void addCartridge() {
System.out.print("加弹夹的-");
}
}

总结

装在 饰者模式在JDK 应用的源码分析:


延伸

    装饰者模式
    装饰器模式-菜鸟教程
    Decorator Design Pattern
    Design Patterns - Decorator Pattern
    设计模式之装饰者模式(Decorator Pattern)
    尚硅谷Java设计模式,韩顺平图解java设计模式

Content
  1. 1. 前言
  2. 2. 装饰者模式
  3. 3. 总结
  4. 4. 延伸