装饰模式

定义与特点

在现实生活中,常常需要对现有产品增加新的功能或美化其外观,如房子装修、相片加相框等。在软件开发过程中,有时想用一些现存的组件。这些组件可能只是完成了一些核心功能。但在不改变其结构的情况下,可以动态地扩展其功能。所有这些都可以釆用装饰模式来实现。

装饰(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。装饰(Decorator)模式的主要优点有:

  • 采用装饰模式扩展对象的功能比采用继承方式更加灵活。
  • 可以设计出多个不同的具体装饰类,创造出多个不同行为的组合。

其主要缺点是:装饰模式增加了许多子类,如果过度使用会使程序变得很复杂。

结构与实现

通常情况下,扩展一个类的功能会使用继承方式来实现。但继承具有静态特征,耦合度高,并且随着扩展功能的增多,子类会很膨胀。如果使用组合关系来创建一个包装对象(即装饰对象)来包裹真实对象,并在保持真实对象的类结构不变的前提下,为其提供额外的功能,这就是装饰模式的目标。

模式的结构

装饰模式主要包含以下角色。

  • 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象。
  • 具体构件(ConcreteComponent)角色:实现抽象构件,通过装饰角色为其添加一些职责。
  • 抽象装饰(Decorator)角色:继承抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
  • 具体装饰(ConcreteDecorator)角色:实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。

模式的实现

装饰模式的实现代码如下:

package Design_Pattern.DecoratorPattern;

/**
 * @author peng
 * @date 20-5-12 下午5:08
 */
public class Decorator_test {
    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()
*/

应用实例

用装饰模式实现游戏角色“龙骑士”的变身。分析:在《Dota2》中,游戏角色“龙骑士”的原身是一个战士,但当他变身时,会变成龙形态,当有神杖会变身黑龙形态。这些都可用装饰模式来实现。

package Design_Pattern.DecoratorPattern;

import javax.swing.*;
import java.awt.*;

/**
 * @author peng
 * @date 20-5-12 下午5:32
 */
public class Dragon_knight {
    public static void main(String[] args) {
        Dragon d1 = new original();
        d1.display();
        Dragon d2 = new Dragonfrom(d1);
        d2.display();
        Dragon d3 = new Black_Dragon(d1);
        d3.display();
    }

}
//抽象构建角色:龙骑士
interface Dragon{
    public void display();
}
//具体构件角色:原画
class original extends JFrame implements Dragon{
    private static final  long serialVersionUID = 1L;
    private String t = "qishi.jpg";
    public original(){
        super("《Dota2》中龙骑士");
    }
    public void setImage(String t){
        this.t = t;
    }
    public void display(){
        this.setLayout(new FlowLayout());
        JLabel l1 = new JLabel(new ImageIcon("/usr/local/github/algo_test/src/Design_Pattern/DecoratorPattern/" + t));
        this.add(l1);
        this.pack();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}
//抽象装饰角色:变形
class Changer implements Dragon{
    Dragon d;
    public Changer(Dragon d){
        this.d = d;
    }
    public void display(){
        d.display();
    }
}
//具体装饰角色:龙形态
class Dragonfrom extends Changer{
    public Dragonfrom(Dragon d){
        super(d);
    }
    public void display(){
        setChanger();
        super.display();
    }
    public void setChanger(){
        ((original) super.d).setImage("long.jpg");
    }
}
//具体装饰角色:黑龙
class Black_Dragon extends Changer
{
    public Black_Dragon(Dragon d)
    {
        super(d);
    }
    public void display()
    {
        setChanger();
        super.display();
    }
    public void setChanger()
    {
        ((original) super.d).setImage("Along.jpg");
    }
}

程序运行结果如下图所示:

应用场景

装饰模式通常在以下几种情况使用。

  • 当需要给一个现有类添加附加职责,而又不能采用生成子类的方法进行扩充时。例如,该类被隐藏或者该类是终极类或者采用继承方式会产生大量的子类。
  • 当需要通过对现有的一组基本功能进行排列组合而产生非常多的功能时,采用继承关系很难实现,而采用装饰模式却很好实现。
  • 当对象的功能要求可以动态地添加,也可以再动态地撤销时。

装饰模式在Java语言中的最著名的应用莫过于 Java I/O 标准库的设计了。例如,InputStream 的子类 FilterInputStream,OutputStream 的子类 FilterOutputStream,Reader 的子类 BufferedReader 以及 FilterReader,还有 Writer 的子类 BufferedWriter、FilterWriter 以及 PrintWriter 等,它们都是抽象装饰类。


   转载规则


《装饰模式》 ForwardPeng 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
外观模式 外观模式
软件设计中,当一个系统的功能越来越强,子系统会越来越多,客户对系统的访问也变得越来越复杂。这时如果系统内部发生改变,客户端也要跟着改变,这违背了“开闭原则”,也违背了“迪米特法则”,所以有必要为多个子系统提供一个统一的接口,从而降低系统的耦
2020-05-12
下一篇 
桥接模式 桥接模式
定义与特点某些类具有两个或多个维度的变化,如图形既可按形状分,又可按颜色分。桥接(Bridge)模式的定义如下:将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。优点是:
2020-05-12
  目录