Skip to content

Commit 63d02bf

Browse files
committed
🎉 decorator pattern
1 parent d3178fe commit 63d02bf

File tree

9 files changed

+387
-0
lines changed

9 files changed

+387
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
---
2+
title: Java设计模式之Decorator Pattern
3+
date: 2019-09-08 21:47:30
4+
tags: [Java,design and pattern]
5+
---
6+
7+
## 学习笔记 : Java设计模式之Decorator Pattern
8+
9+
### 概述
10+
*装饰设计模式( 对象结构型模式 )是一种用于替代继承的技术,它通过一种无须定义子类的方式来给对象动态增加职责,使用对象之间的关联关系取代类之间的继承关系. 装饰设计模式的定位为 : 动态地给一个对象增加一些额外的职责. 就扩展而言,装饰模式提供了一种比使用子类更加灵活的替代方案. ( Decorator Pattern : Attach additonal responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality )*
11+
12+
13+
### 装饰设计模式中的角色
14+
1. *`Component ( 抽象构件 )` : 它是`具体构件``抽象装饰类`的共同父类,声明了在具体构件中实现的业务方法,它的引入可以使客户端以一致的方式处理未被装饰的对象及装饰之后的对象,实现客户端的透明操作.*
15+
2. *`ConcreteComponent ( 具体构件 )` : 它是`抽象构件类`的子类,用于定义具体的构件对象,实现了在抽象构件中声明的方法,装饰类可以给它增加额外的职责( 方法 )*
16+
3. *`Decorator ( 抽象装饰类 )` : 它也是`抽象构件类`的子类,用于给`具体构件`增加职责,但是具体职责在其子类中实现. 它维护一个指向抽象构件的引用,通过该引用可以调用装饰之前构件对象的方法,并通过子类扩展该方法,继而达到装饰的目的.*
17+
4. *`ConcreteDecorator ( 具体装饰类 )` : 它是`抽象装饰类`的子类,负责向构件添加新的职责. 每一个具体装饰类都定义了一些新的行为,它可以调用在抽象装饰类中定义的方法,并可以增加新的方法用于扩展对象的行为.*
18+
19+
20+
### 示例程序-透明装饰模式
21+
*下面是一个使用了装饰设计模式的简单示例程序,其示例说明为 : 使用面向对象技术开发了一套图形界面构件库,该构件库提供了大量的基本结构,如窗体,文本框,列表框等,由于在使用时用户经常要求定制一些特殊的显示效果,如带滚动条的窗体,或带黑色边框的文本框等. 因此经常需要对该构件库进行扩展以增强其功能,既而可以使用装饰设计模式来设计该界面构件库哟~*
22+
23+
1. *Component接口 : 抽象界面构件类, 充当抽象构件类*
24+
```java
25+
package pers.huangyuhui.decorator_pattern;
26+
27+
/**
28+
* @project: design-patterns
29+
* @description: 抽象界面构件类, 充当抽象构件类
30+
* @author: 黄宇辉
31+
* @date: 9/9/2019-2:47 PM
32+
* @version: 1.0
33+
* @website: https://yubuntu0109.github.io/
34+
*/
35+
public abstract class Component {
36+
37+
public abstract void display();
38+
}
39+
```
40+
41+
2. *Window类 : 窗体类, 充当具体构件类*
42+
```java
43+
package pers.huangyuhui.decorator_pattern;
44+
45+
/**
46+
* @project: design-patterns
47+
* @description: 窗体类, 充当具体构件类
48+
* @author: 黄宇辉
49+
* @date: 9/9/2019-2:48 PM
50+
* @version: 1.0
51+
* @website: https://yubuntu0109.github.io/
52+
*/
53+
public class Window extends Component {
54+
55+
@Override
56+
public void display() {
57+
System.out.println("显示窗体 !");
58+
}
59+
}
60+
```
61+
62+
3. *TextBox类 : 文本框类, 充当具体构件类*
63+
```java
64+
package pers.huangyuhui.decorator_pattern;
65+
66+
/**
67+
* @project: design-patterns
68+
* @description: 文本框类, 充当具体构件类
69+
* @author: 黄宇辉
70+
* @date: 9/9/2019-2:50 PM
71+
* @version: 1.0
72+
* @website: https://yubuntu0109.github.io/
73+
*/
74+
public class TextBox extends Component {
75+
@Override
76+
public void display() {
77+
System.out.println("显示文本框 !");
78+
}
79+
}
80+
```
81+
82+
4. *ListBox类 : 列表框类, 充当具体构件类*
83+
```java
84+
package pers.huangyuhui.decorator_pattern;
85+
86+
/**
87+
* @project: design-patterns
88+
* @description: 列表框类, 充当具体构件类
89+
* @author: 黄宇辉
90+
* @date: 9/9/2019-2:51 PM
91+
* @version: 1.0
92+
* @website: https://yubuntu0109.github.io/
93+
*/
94+
public class ListBox extends Component {
95+
96+
@Override
97+
public void display() {
98+
System.out.println("显示列表框 !");
99+
}
100+
}
101+
```
102+
103+
5. *ComponentDecorator类 : 构件装饰类, 充当抽象装饰类*
104+
```java
105+
package pers.huangyuhui.decorator_pattern;
106+
107+
/**
108+
* @project: design-patterns
109+
* @description: 构件装饰类, 充当抽象装饰类
110+
* @author: 黄宇辉
111+
* @date: 9/9/2019-2:52 PM
112+
* @version: 1.0
113+
* @website: https://yubuntu0109.github.io/
114+
*/
115+
public class ComponentDecorator extends Component {
116+
117+
//维持对抽象构件类型对象的引用
118+
private Component component;
119+
120+
//注入抽象构件类型的对象
121+
public ComponentDecorator(Component component) {
122+
this.component = component;
123+
}
124+
125+
@Override
126+
public void display() {
127+
component.display();
128+
}
129+
}
130+
```
131+
132+
6. *ScrollBarDecorator类 : 滚动条装饰类, 充当具体装饰类*
133+
```java
134+
package pers.huangyuhui.decorator_pattern;
135+
136+
/**
137+
* @project: design-patterns
138+
* @description: 滚动条装饰类, 充当具体装饰类
139+
* @author: 黄宇辉
140+
* @date: 9/9/2019-2:54 PM
141+
* @version: 1.0
142+
* @website: https://yubuntu0109.github.io/
143+
*/
144+
public class ScrollBarDecorator extends ComponentDecorator {
145+
146+
public ScrollBarDecorator(Component component) {
147+
super(component);
148+
}
149+
150+
@Override
151+
public void display() {
152+
this.setScrollBar();
153+
super.display();
154+
}
155+
156+
private void setScrollBar() {
157+
System.out.println("为构件添加滚动条 !");
158+
}
159+
}
160+
```
161+
162+
7. *BlackBorderDecorator类 : 黑色边框装饰类, 充当具体装饰类*
163+
```java
164+
package pers.huangyuhui.decorator_pattern;
165+
166+
/**
167+
* @project: design-patterns
168+
* @description: 黑色边框装饰类, 充当具体装饰类
169+
* @author: 黄宇辉
170+
* @date: 9/9/2019-2:59 PM
171+
* @version: 1.0
172+
* @website: https://yubuntu0109.github.io/
173+
*/
174+
public class BlackBorderDecorator extends ComponentDecorator {
175+
176+
public BlackBorderDecorator(Component component) {
177+
super(component);
178+
}
179+
180+
@Override
181+
public void display() {
182+
this.setBlackBorder();
183+
super.display();
184+
}
185+
186+
private void setBlackBorder() {
187+
System.out.println("为构件添加黑色边框 !");
188+
}
189+
}
190+
```
191+
192+
8. *Client类 : 客户端测试类*
193+
```java
194+
package pers.huangyuhui.decorator_pattern;
195+
196+
/**
197+
* @project: design-patterns
198+
* @description: 客户端测试类
199+
* @author: 黄宇辉
200+
* @date: 9/9/2019-3:03 PM
201+
* @version: 1.0
202+
* @website: https://yubuntu0109.github.io/
203+
*/
204+
public class Client {
205+
206+
public static void main(String[] args) {
207+
Component component = new Window();
208+
component.display();
209+
210+
System.out.println("------------------");
211+
212+
Component component2 = new ScrollBarDecorator(component);
213+
component2.display();
214+
}
215+
}
216+
```
217+
218+
#### 示例程序的类图
219+
![ ](https://raw.githubusercontent.com/YUbuntu0109/YUbuntu0109.github.io/HexoBackup/source/_posts/Java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F%E4%B9%8BDecorator-Pattern/Decorator-Pattern-ClassDiagram.png)
220+
221+
222+
223+
### 示例程序-半透明装饰模式
224+
*暂略 · · ·*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 黑色边框装饰类, 充当具体装饰类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:59 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class BlackBorderDecorator extends ComponentDecorator {
12+
13+
public BlackBorderDecorator(Component component) {
14+
super(component);
15+
}
16+
17+
@Override
18+
public void display() {
19+
this.setBlackBorder();
20+
super.display();
21+
}
22+
23+
private void setBlackBorder() {
24+
System.out.println("为构件添加黑色边框 !");
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 客户端测试类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-3:03 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Client {
12+
13+
public static void main(String[] args) {
14+
Component component = new Window();
15+
component.display();
16+
17+
System.out.println("------------------");
18+
19+
Component component2 = new ScrollBarDecorator(component);
20+
component2.display();
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 抽象界面构件类, 充当抽象构件类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:47 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public abstract class Component {
12+
public abstract void display();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 构件装饰类, 充当抽象装饰类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:52 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class ComponentDecorator extends Component {
12+
13+
//维持对抽象构件类型对象的引用
14+
private Component component;
15+
16+
//注入抽象构件类型的对象
17+
public ComponentDecorator(Component component) {
18+
this.component = component;
19+
}
20+
21+
@Override
22+
public void display() {
23+
component.display();
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 列表框类, 充当具体构件类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:51 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class ListBox extends Component {
12+
13+
@Override
14+
public void display() {
15+
System.out.println("显示列表框 !");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 滚动条装饰类, 充当具体装饰类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:54 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class ScrollBarDecorator extends ComponentDecorator {
12+
13+
public ScrollBarDecorator(Component component) {
14+
super(component);
15+
}
16+
17+
@Override
18+
public void display() {
19+
this.setScrollBar();
20+
super.display();
21+
}
22+
23+
private void setScrollBar() {
24+
System.out.println("为构件添加滚动条 !");
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 文本框类, 充当具体构件类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:50 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class TextBox extends Component {
12+
13+
@Override
14+
public void display() {
15+
System.out.println("显示文本框 !");
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package pers.huangyuhui.decorator_pattern.decorator_pattern_1;
2+
3+
/**
4+
* @project: design-patterns
5+
* @description: 窗体类, 充当具体构件类
6+
* @author: 黄宇辉
7+
* @date: 9/9/2019-2:48 PM
8+
* @version: 1.0
9+
* @website: https://yubuntu0109.github.io/
10+
*/
11+
public class Window extends Component {
12+
13+
@Override
14+
public void display() {
15+
System.out.println("显示窗体 !");
16+
}
17+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy