通过使用修饰模式,可以在运行时扩充一个类别的功能。原理是:增加一个修饰类包裹原来的类别,包裹的方式是在修饰类的构造函数中将原来的类以参数的形式传入。装饰类实现新的功能,但是,在不需要用到新功能的地方,它可以直接调用原来的类别中的方法。修饰类必须和原来的类别有相同的接口。
装饰器
UML类图
代码实现
Component类
1 2 3 4 5
| package com.lei.designpatterns.decorator;
public abstract class Component { public abstract void operation(); }
|
ConcreteComponent类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class ConcreteComponent extends Component{
@Override public void operation() { System.out.println("Concrete component."); } }
|
Decorator类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.lei.designpatterns.decorator;
public class Decorator extends Component{ protected Component component;
public void setComponent(Component component){ this.component = component; }
@Override public void operation() { if (this.component != null){ this.component.operation(); } } }
|
ConcreteDecoratorA类
1 2 3 4 5 6 7 8 9 10 11 12
| package com.lei.designpatterns.decorator;
public class ConcreteDecoratorA extends Decorator{ private String addedState;
@Override public void operation() { super.operation(); this.addedState = "New state."; System.out.println("New Operation a."); } }
|
ConcreteDecoratorB
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.lei.designpatterns.decorator;
public class ConcreteDecoratorB extends Decorator{ @Override public void operation() { super.operation(); addedBehavior(); System.out.println("New Operation b."); }
private void addedBehavior(){ } }
|
装饰器样例
UML类图
代码实现
Person类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.lei.designpatterns.decorator;
public class Person { private String name;
public Person(){ }
public Person(String name){ this.name = name; }
public void show(){ System.out.println("Show."); } }
|
Finery类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.lei.designpatterns.decorator;
public class Finery extends Person{ private Person component;
public void Decorate(Person component){ this.component = component; }
@Override public void show() { if (this.component != null){ this.component.show(); } } }
|
TShirts类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class TShirts extends Finery { @Override public void show() { System.out.println("T shirts."); super.show(); } }
|
BigTrouser类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class BigTrouser extends Finery { @Override public void show() { System.out.println("Big trouser."); super.show(); } }
|
BrokenSneaker类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class BrokenSneaker extends Finery { @Override public void show() { System.out.println("Broken sneaker."); super.show(); } }
|
Suite类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class Suite extends Finery { @Override public void show() { System.out.println("Suite."); super.show(); } }
|
Tie类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class Tie extends Finery { @Override public void show() { System.out.println("Tie."); super.show(); } }
|
LeatherShoes类
1 2 3 4 5 6 7 8 9
| package com.lei.designpatterns.decorator;
public class LeatherShoes extends Finery { @Override public void show() { System.out.println("Leather shoes."); super.show(); } }
|