Skip to content

Commit 3134fb3

Browse files
authored
装饰模式
装饰模式
1 parent a3c44ae commit 3134fb3

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.java.design.decorator;
2+
3+
public abstract class Decorator implements Person {
4+
5+
protected Person person;
6+
7+
public void setPerson(Person person) {
8+
this.person = person;
9+
}
10+
11+
@Override
12+
public void eat() {
13+
person.eat();
14+
}
15+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.java.design.decorator;
2+
3+
/**
4+
* 装饰模式 -----> 对新房进行装修并没有改变房屋的本质,但它可以让房子变得更漂亮、更温馨、更实用。
5+
* 在软件设计中,对已有对象(新房)的功能进行扩展(装修)。 把通用功能封装在装饰器中,用到的地方进行调用。
6+
* 装饰模式是一种用于替代继承的技术,使用对象之间的关联关系取代类之间的继承关系。引入装饰类,扩充新功能。
7+
*
8+
* @author Administrator
9+
*
10+
*/
11+
public class DecoratorPattern {
12+
13+
public static void main(String[] args) {
14+
15+
Man man = new Man();
16+
ManDecoratorA decoratorA = new ManDecoratorA();
17+
ManDecoratorB decoratorB = new ManDecoratorB();
18+
19+
decoratorA.setPerson(man);
20+
// decoratorB.setPerson(decoratorA);
21+
decoratorA.eat();
22+
23+
}
24+
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.java.design.decorator;
2+
3+
public class Man implements Person {
4+
5+
@Override
6+
public void eat() {
7+
8+
System.out.println("Man eating ...");
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.java.design.decorator;
2+
3+
public class ManDecoratorA extends Decorator {
4+
5+
@Override
6+
public void eat() {
7+
super.eat();
8+
reEat();
9+
System.out.println("Decorator A ...");
10+
}
11+
12+
public void reEat() {
13+
System.out.println("Eat again ...");
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.java.design.decorator;
2+
3+
public class ManDecoratorB extends Decorator {
4+
5+
@Override
6+
public void eat() {
7+
super.eat();
8+
9+
System.out.println("Man DecoratorB ...");
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.java.design.decorator;
2+
3+
public interface Person {
4+
5+
void eat();
6+
7+
}

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