Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d3693e4

Browse files
author
deeper
committed
update decorator
1 parent fb7898f commit d3693e4

File tree

7 files changed

+95
-0
lines changed

7 files changed

+95
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ruoxu.pattern.decorator;
2+
3+
public class Boy implements Person{
4+
5+
@Override
6+
public void dressed() {
7+
System.out.println("boy:穿上内裤");
8+
}
9+
10+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.ruoxu.pattern.decorator;
2+
3+
public class CheapClothDecorator extends ClothDecorator{
4+
5+
public CheapClothDecorator(Person person) {
6+
super(person);
7+
}
8+
9+
@Override
10+
public void dressed() {
11+
super.dressed();
12+
dressShorts();
13+
}
14+
15+
private void dressShorts() {
16+
System.out.println("穿件短裤");
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.ruoxu.pattern.decorator;
2+
3+
public abstract class ClothDecorator implements Person{
4+
private Person mPerson; // 保持一个Person类的引用
5+
6+
public ClothDecorator(Person person) {
7+
this.mPerson = person;
8+
}
9+
10+
@Override
11+
public void dressed() {
12+
mPerson.dressed();
13+
}
14+
15+
}

‎src/com/ruoxu/pattern/decorator/Demo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010
*/
1111
public class Demo {
1212
public static void main(String[] args) {
13+
Person boy = new Boy();
14+
ClothDecorator boyDecorator1 = new ExpensiveClothDecorator(boy);
15+
boyDecorator1.dressed();
16+
17+
System.out.println();
18+
19+
ClothDecorator boyDecorator2 = new CheapClothDecorator(boy);
20+
boyDecorator2.dressed();
21+
22+
System.out.println();
23+
24+
// Person girl = new Girl();
25+
// ClothDecorator girlDecorator = new ExpensiveClothDecorator(girl);
26+
// girlDecorator.dressed();
1327

1428
}
1529
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.ruoxu.pattern.decorator;
2+
3+
public class ExpensiveClothDecorator extends ClothDecorator{
4+
5+
public ExpensiveClothDecorator(Person person) {
6+
super(person);
7+
}
8+
9+
@Override
10+
public void dressed() {
11+
dressedJean();
12+
super.dressed(); // 位置可在前中后。
13+
dressShirt();
14+
}
15+
16+
private void dressShirt() {
17+
System.out.println("穿件短袖");
18+
}
19+
20+
private void dressedJean() {
21+
System.out.println("穿上牛仔裤");
22+
}
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ruoxu.pattern.decorator;
2+
3+
public class Girl implements Person{
4+
5+
@Override
6+
public void dressed() {
7+
System.out.println("girl:戴个胸罩");
8+
}
9+
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.ruoxu.pattern.decorator;
2+
3+
public interface Person {
4+
void dressed();
5+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /