装饰器模式的结构与实现
通常情况下,扩展一个类的功能会使用继承方式来实现。但继承具有静态特征,耦合度高,并且随着扩展功能的增多,子类会很膨胀。如果使用组合关系来创建一个包装对象(即装饰对象)来包裹真实对象,并在保持真实对象的类结构不变的前提下,为其提供额外的功能,这就是装饰器模式的目标。下面来分析其基本结构和实现方法。
1. 模式的结构
装饰器模式主要包含以下角色。
- 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象。
- 具体构件(ConcreteComponent)角色:实现抽象构件,通过装饰角色为其添加一些职责。
- 抽象装饰(Decorator)角色:继承抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
- 具体装饰(ConcreteDecorator)角色:实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
装饰器模式的结构图如图 1 所示。
图1 装饰器模式的结构图
2. 模式的实现
装饰器模式的实现代码如下:
package decorator;
public class DecoratorPattern {
public static void main(String[] args) {
Component p = new ConcreteComponent();
p.operation();
System.out.println("---------------------------------");
Component d = new ConcreteDecorator(p);
d.operation();
}
}
//抽象构件角色
interface Component {
public void operation();
}
//具体构件角色
class ConcreteComponent implements Component {
public ConcreteComponent() {
System.out.println("创建具体构件角色");
}
public void operation() {
System.out.println("调用具体构件角色的方法operation()");
}
}
//抽象装饰角色
class Decorator implements Component {
private Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
}
}
//具体装饰角色
class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component component) {
super(component);
}
public void operation() {
super.operation();
addedFunction();
}
public void addedFunction() {
System.out.println("为具体构件角色增加额外的功能addedFunction()");
}
}
程序运行结果如下:
创建具体构件角色
调用具体构件角色的方法operation()
---------------------------------
调用具体构件角色的方法operation()
为具体构件角色增加额外的功能addedFunction()
装饰器模式的应用实例
【例1】用装饰器模式实现游戏角色“莫莉卡·安斯兰”的变身。
分析:在《恶魔战士》中,游戏角色“莫莉卡·安斯兰”的原身是一个可爱少女,但当她变身时,会变成头顶及背部延伸出蝙蝠状飞翼的女妖,当然她还可以变为穿着漂亮外衣的少女。这些都可用装饰器模式来实现,在本实例中的“莫莉卡”原身有 setImage(String t) 方法决定其显示方式,而其 变身“蝙蝠状女妖”和“着装少女”可以用 setChanger() 方法来改变其外观,原身与变身后的效果用 display() 方法来显示(点此下载其原身和变身后的图片),图 2 所示是其结构图。
图2 游戏角色“莫莉卡·安斯兰”的结构图
程序代码如下:
package decorator;
import java.awt.*;
import javax.swing.*;
public class MorriganAensland {
public static void main(String[] args) {
Morrigan m0 = new original();
m0.display();
Morrigan m1 = new Succubus(m0);
m1.display();
Morrigan m2 = new Girl(m0);
m2.display();
}
}
//抽象构件角色:莫莉卡
interface Morrigan {
public void display();
}
//具体构件角色:原身
class original extends JFrame implements Morrigan {
private static final long serialVersionUID = 1L;
private String t = "Morrigan0.jpg";
public original() {
super("《恶魔战士》中的莫莉卡·安斯兰");
}
public void setImage(String t) {
this.t = t;
}
public void display() {
this.setLayout(new FlowLayout());
JLabel l1 = new JLabel(new ImageIcon("src/decorator/" + t));
this.add(l1);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//抽象装饰角色:变形
class Changer implements Morrigan {
Morrigan m;
public Changer(Morrigan m) {
this.m = m;
}
public void display() {
m.display();
}
}
//具体装饰角色:女妖
class Succubus extends Changer {
public Succubus(Morrigan m) {
super(m);
}
public void display() {
setChanger();
super.display();
}
public void setChanger() {
((original) super.m).setImage("Morrigan1.jpg");
}
}
//具体装饰角色:少女
class Girl extends Changer {
public Girl(Morrigan m) {
super(m);
}
public void display() {
setChanger();
super.display();
}
public void setChanger() {
((original) super.m).setImage("Morrigan2.jpg");
}
}
程序运行结果如图 3 所示。
图3 游戏角色“莫莉卡·安斯兰”的变身
装饰器模式的应用场景
前面讲解了关于装饰器模式的结构与特点,下面介绍其适用的应用场景,装饰器模式通常在以下几种情况使用。
- 当需要给一个现有类添加附加职责,而又不能采用生成子类的方法进行扩充时。例如,该类被隐藏或者该类是终极类或者采用继承方式会产生大量的子类。
- 当需要通过对现有的一组基本功能进行排列组合而产生非常多的功能时,采用继承关系很难实现,而采用装饰器模式却很好实现。
- 当对象的功能要求可以动态地添加,也可以再动态地撤销时。
装饰器模式在 Java 语言中的最著名的应用莫过于 Java I/O 标准库的设计了。例如,InputStream 的子类 FilterInputStream,OutputStream 的子类 FilterOutputStream,Reader 的子类 BufferedReader 以及 FilterReader,还有 Writer 的子类 BufferedWriter、FilterWriter 以及 PrintWriter 等,它们都是抽象装饰类。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)