Skip to content

Commit f8ab59e

Browse files
committed
add > 装饰着模式
1 parent 7ea9bb6 commit f8ab59e

2 files changed

Lines changed: 119 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
- [外观(门面)模式(Facade)](https://github.com/lihengming/java-codes/blob/master/design-pattern/src/main/java/structural/FacadePattern.java)
3030
- [适配器模式(Adapter)](https://github.com/lihengming/java-codes/blob/master/design-pattern/src/main/java/structural/AdapterPattern.java)
3131
- [代理模式(Proxy)](https://github.com/lihengming/java-codes/blob/master/design-pattern/src/main/java/structural/ProxyPattern.java)
32-
- 装饰模式(Decorator)
33-
- 桥模式(Bridge)
32+
- [装饰模式(Decorator)](https://github.com/lihengming/java-codes/blob/master/design-pattern/src/main/java/structural/DecoratorPattern.java)
33+
- 桥接模式(Bridge)
3434
- 组合模式(Composite)
3535
- 享元模式(Flyweight)
3636
- [行为型模式(Behavioral)](https://github.com/lihengming/java-codes/tree/master/design-pattern/src/main/java/behavioral)
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package structural;
2+
3+
/**
4+
* Created by 李恒名 on 2017/7/27.
5+
* <p>
6+
* 装饰者模式
7+
*
8+
* @see java.io.InputStream 抽象构件
9+
* @see java.io.FilterInputStream 抽象装饰类
10+
* @see java.io.BufferedInputStream 具体的装饰类
11+
* @see java.io.DataInputStream 具体的装饰类
12+
*/
13+
public class DecoratorPattern {
14+
//抽象构件(Component)
15+
static abstract class Man {
16+
abstract void aboutMe();
17+
}
18+
19+
//具体的构件(ConcreteComponent)
20+
static class Potato extends Man {
21+
22+
@Override
23+
public void aboutMe() {
24+
System.out.print("Hi,我是土豆");
25+
}
26+
}
27+
28+
//抽象装饰类(Decorator)
29+
static abstract class Decorator extends Man {
30+
private Man man;
31+
32+
protected Decorator(Man man) {
33+
this.man = man;
34+
}
35+
36+
@Override
37+
public void aboutMe() {
38+
man.aboutMe();
39+
}
40+
}
41+
42+
//具体装饰类(ConcreteDecorator)
43+
static class ProgrammerDecorator extends Decorator {
44+
public ProgrammerDecorator(Man man) {
45+
super(man);
46+
}
47+
48+
@Override
49+
public void aboutMe() {
50+
super.aboutMe();
51+
System.out.print(",我的职业是程序员");
52+
}
53+
}
54+
55+
//具体装饰类(ConcreteDecorator)
56+
static class ReaderDecorator extends Decorator {
57+
public ReaderDecorator(Man man) {
58+
super(man);
59+
}
60+
61+
@Override
62+
public void aboutMe() {
63+
super.aboutMe();
64+
System.out.print(",我喜欢读书");
65+
66+
}
67+
}
68+
69+
//具体装饰类(ConcreteDecorator)
70+
static class GitHubDecorator extends Decorator {
71+
public GitHubDecorator(Man man) {
72+
super(man);
73+
}
74+
75+
@Override
76+
public void aboutMe() {
77+
super.aboutMe();
78+
System.out.print(",闲暇时我喜欢在 GitHub 上溜达");
79+
80+
}
81+
}
82+
83+
//具体装饰类(ConcreteDecorator)
84+
static class WriterDecorator extends Decorator {
85+
public WriterDecorator(Man man) {
86+
super(man);
87+
}
88+
89+
@Override
90+
public void aboutMe() {
91+
super.aboutMe();
92+
System.out.print(",也会去写一些技术文章");
93+
94+
}
95+
}
96+
97+
//具体装饰类(ConcreteDecorator)
98+
public static void main(String[] args) {
99+
Man potato = new Potato();
100+
potato.aboutMe();
101+
//Hi,我是土豆
102+
103+
System.out.println();
104+
105+
potato = new WriterDecorator(new GitHubDecorator(new ReaderDecorator(new ProgrammerDecorator(potato))));
106+
potato.aboutMe();
107+
//Hi,我是土豆,我的职业是程序员,我喜欢读书,闲暇时我喜欢在 GitHub 上溜达,也会去写一些技术文章
108+
}
109+
110+
/**
111+
* 总结:
112+
* 装饰者模式,可以动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更加有
113+
* 弹性的替代方案,有效的避免了类爆炸现象的产生。虽然装饰者模式能够动态将责任附加到
114+
* 对象上,但是他会产生许多的细小对象,增加了系统的复杂度。java.io 包可谓是装饰者
115+
* 模式的最佳实践。
116+
*/
117+
}

0 commit comments

Comments
 (0)