File tree Expand file tree Collapse file tree 2 files changed +75
-1
lines changed
docs/java-tutor/design-pattern Expand file tree Collapse file tree 2 files changed +75
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments