设计模式:装饰模式(Decorator )

定义与结构

装饰模式(Decorator)也叫包装器模式(Wrapper)。GOF 在《设计模式》一书中给出的定义为:动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator 模式相比生成子类更为灵活。

让我们来理解一下这句话。我们来设计“门”这个类。假设你根据需求为“门”类作了如下
定义:
这里写图片描述
现在,在系统的一个地方需要一个能够报警的Door,你来怎么做呢?你或许写一个Door的子类AlarmDoor,在里面添加一个子类独有的方法alarm()。嗯,那在使用警报门的地方你必须让客户知道使用的是警报门,不然无法使用这个独有的方法。而且,这个还违反了Liskov 替换原则。

也许你要说,那就把这个方法添加到Door 里面,这样不就统一了?但是这样所有的门都必须有警报,至少是个“哑巴”警报。而当你的系统仅仅在一两个地方使用了警报门,这明显是不合理的——虽然可以使用缺省适配器来弥补一下。

这时候,你可以考虑采用装饰模式来给门动态的添加些额外的功能。
下面我们来看看装饰模式的组成,不要急着去解决上面的问题,到了下面自然就明白了!
1) 抽象构件角色(Component):定义一个抽象接口,以规范准备接收附加责任的对象。
2) 具体构件角色(Concrete Component):这是被装饰者,定义一个将要被装饰增加功能的类。
3) 装饰角色(Decorator):持有一个构件对象的实例,并定义了抽象构件定义的接口。
4) 具体装饰角色(Concrete Decorator):负责给构件添加增加的功能。
看下装饰模式的类图:
这里写图片描述

图中 ConcreteComponent 可能继承自其它的体系,而为了实现装饰模式,他还要实现Component 接口。整个装饰模式的结构是按照组合模式来实现的——两者都有类似的结构图,都基于递归组合来组织可变数目的对象。但是两者的目的是截然不同的,组合(Composite)模式侧重通过递归组合构造类,使不同的对象、多重的对象可以“一视同仁”;而装饰(Decorator)模式仅仅是借递归组合来达到定义中的目的。

装饰模式的实现

//抽象构件角色--Component
public interface Shape {
    void draw();
}
//具体构件角色--Concrete ComponentA
public class Circle implements Shape {

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println("Shape: Circle");
    }

}
//具体构件角色--Concrete ComponentB
public class Rectangle implements Shape {

    @Override
    public void draw() {
        // TODO Auto-generated method stub
        System.out.println("Shape: Rectangle");
    }

}
//装饰角色--Decorator
public abstract class ShapeDecorator implements Shape{
    protected Shape decoratedShape;

    public ShapeDecorator(Shape decoratedShape) {
        this.decoratedShape = decoratedShape;
    }

    public void draw() {
        decoratedShape.draw();
    }
}
//具体装饰角色--Concrete Decorator
public class RedShapeDecorator extends ShapeDecorator {

    public RedShapeDecorator(Shape decoratedShape) {
        super(decoratedShape);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void draw() {
        decoratedShape.draw();
        setRedBorder(decoratedShape);
    }

    private void setRedBorder(Shape decoratedShape) {
        System.out.println("Border Color: Red");
    }

}

//客户端
public class DecoratorPatternDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Shape circle = new Circle();

        Shape redCircle = new RedShapeDecorator(new Circle());

        Shape redRectangle = new RedShapeDecorator(new Rectangle());
        System.out.println("Circle with normal border");
        circle.draw();

        System.out.println("\nCircle of red border");
        redCircle.draw();

        System.out.println("\nRectangle of red border");
        redRectangle.draw();
    }

}

验证输出

Circle with normal border
Shape: Circle

Circle of red border
Shape: Circle
Border Color: Red

Rectangle of red border
Shape: Rectangle
Border Color: Red
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章