Java装饰模式
装饰模式:给一个类添加一些额外的职责,并且在添加这些额外的职责时不会控制该类的执行逻辑。
UML类图:
组成部分:
抽象构件:原始的功能接口
具体构件:具体的原始功能类
装饰角色:持有具体构件类的对象,以便执行原有功能
具体装饰:具体扩展的功能在这里
下面看一个对开车功能拓展的实例(晚上+开车):
抽象构件:
Java代码
1.package com.gjy.drector;
2.
3./**
4. * 抽象接口,规范准备接收附加责任的对象
5. * @author gjy
6. */
7.public interface Component {
8. public void operation();
9.}
package com.gjy.drector;
/**
* 抽象接口,规范准备接收附加责任的对象
* @author gjy
*/
public interface Component {
public void operation();
}
具体构件:
Java代码
1.package com.gjy.drector;
2.
3./**
4. * 接收附加责任, 此类型的类可以有多个, 只对应一个Decorator类
5. * @author gjy
6. */
7.public class ConcreteComponent implements Component {
8. public ConcreteComponent(){}
9. public void operation()
10. {
11. System.out.println("开车");
12. }
13.}
package com.gjy.drector;
/**
* 接收附加责任, 此类型的类可以有多个, 只对应一个Decorator类
* @author gjy
*/
public class ConcreteComponent implements Component {
public ConcreteComponent(){}
public void operation()
{
System.out.println("开车");
}
}
装饰角色:
Java代码
1.package com.gjy.drector;
2.
3./**
4. * 装饰角色,持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口
5. * @author gjy
6. */
7.public class Decorator implements Component {
8. private Component component;
9. public Decorator(){}
10.
11. public Decorator(Component component)
12. {
13. this.component = component;
14. }
15.
16. public void operation() {
17. component.operation();
18. }
19.}
package com.gjy.drector;
/**
* 装饰角色,持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口
* @author gjy
*/
public class Decorator implements Component {
private Component component;
public Decorator(){}
public Decorator(Component component)
{
this.component = component;
}
public void operation() {
component.operation();
}
}
具体装饰:
Java代码
1.package com.gjy.drector;
2.
3./**
4. * 添加附加责任
5. * @author gjy
6. */
7.public class ConcreteDecorator extends Decorator {
8.
9. public ConcreteDecorator(){}
10.
11. public ConcreteDecorator(Component component)
12. {
13. super(component);
14. }
15.
16. public void operation()
17. {
18. this.addedOperation();
19. super.operation();
20. }
21.
22. public void addedOperation()
23. {
24. System.out.println("晚上");
25. }
26.}
package com.gjy.drector;
/**
* 添加附加责任
* @author gjy
*/
public class ConcreteDecorator extends Decorator {
public ConcreteDecorator(){}
public ConcreteDecorator(Component component)
{
super(component);
}
public void operation()
{
this.addedOperation();
super.operation();
}
public void addedOperation()
{
System.out.println("晚上");
}
}
测试:
Java代码
1.package com.gjy.drector;
2.
3./**
4. * 客户端类
5. * @author gjy
6. */
7.
8.public class Client {
9. public static void main(String[] args) {
10. Component component = new ConcreteComponent();
11. Decorator decorator = new ConcreteDecorator(component);
12. //客户端不变, 但已增加了责任
13. decorator.operation();
14.
15. }
16.}
package com.gjy.drector;
/**
* 客户端类
* @author gjy
*/
public class Client {
public static void main(String[] args) {
Component component = new ConcreteComponent();
Decorator decorator = new ConcreteDecorator(component);
//客户端不变, 但已增加了责任
decorator.operation();
}
}
输出结果:
晚上
开车
|