Skip to content

Commit 9de892b

Browse files
committed
add design pattern decorator
1 parent 02d80e1 commit 9de892b

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

.github/workflows/deploy_gh_pages.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ jobs:
2121

2222
- name: 设置 pnpm
2323
uses: pnpm/action-setup@v4
24-
24+
with:
25+
version: 10
2526

2627
- name: 设置 Node.js
2728
uses: actions/setup-node@v4
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# 装饰器模式
2+
3+
```java
4+
5+
@Slf4j
6+
class DecoratorPatternTest {
7+
@Test
8+
void test() {
9+
Tiger tiger = new Tiger();
10+
Living flyDecorator = new ThrowStoneDecorator(new FlyDecorator(tiger));
11+
flyDecorator.behave();
12+
}
13+
}
14+
15+
interface Living {
16+
void behave();
17+
}
18+
19+
class Tiger implements Living {
20+
21+
22+
@Override
23+
public void behave() {
24+
System.out.println("tiger behave");
25+
}
26+
}
27+
28+
class Bird implements Living {
29+
@Override
30+
public void behave() {
31+
System.out.println("bird behave");
32+
}
33+
}
34+
35+
abstract class LivingDecorator implements Living {
36+
Living living;
37+
38+
public LivingDecorator(Living living) {
39+
this.living = living;
40+
}
41+
42+
@Override
43+
public void behave() {
44+
this.living.behave();
45+
}
46+
}
47+
48+
class FlyDecorator extends LivingDecorator {
49+
50+
public FlyDecorator(Living living) {
51+
super(living);
52+
}
53+
54+
@Override
55+
public void behave() {
56+
super.behave();
57+
System.out.println("i can fly");
58+
}
59+
}
60+
61+
class ThrowStoneDecorator extends LivingDecorator {
62+
public ThrowStoneDecorator(Living living) {
63+
super(living);
64+
}
65+
66+
@Override
67+
public void behave() {
68+
super.behave();
69+
System.out.println("i can throw stone");
70+
}
71+
}
72+
73+
```

0 commit comments

Comments
 (0)