装饰器模式(包装器模式)

参考书籍:《java设计模式》刘伟

开发了一套图形界面构件库VisualComponent,该构件库提供
了大量基本构件,如窗体、文本框、列表框等,由于在使用该构件库时,用户经常要求定制
一些特效显示效果,如带滚动条的窗体、带黑色边框的文本框、既带滚动条又带黑色边框的
列表框等等,因此经常需要对该构件库进行扩展以增强其功能:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6V6s3nnL-1584001371013)(0515EFC8E9E94546A7567B52DED6CAAC)]

这样设计:系统扩展麻烦,代码重复,系统庞大。

通过装饰者模式的设计后:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BypZ2DNo-1584001371014)(6EB281D3B1F34D9499E8DDC9CD49654C)]

Component充当抽象构件类,其子类Window、TextBox、ListBox充当具体构件类,Component类的另一个子类ComponentDecorator充当抽象装饰类,ComponentDecorator的子类ScrollBarDecorator和BlackBorderDecorator充当具体装饰类。完整代码如下所示:

//抽象界面构件类:抽象构件类,为了突出与模式相关的核心代码,对原有控件代码进行了大量的简化
abstract class Component{
    public abstract void display();
}
//窗体类:具体构件类
class Window extends Component{
    public void display(){
    System.out.println("显示窗体!");
    }
}
//文本框类:具体构件类
class TextBox extends Component{
    public void display(){
    System.out.println("显示文本框!");
    }
}

//列表框类:具体构件类
class ListBox extends Component{
    public void display(){
    System.out.println("显示列表框!");
    }
}
//构件装饰类:抽象装饰类
class ComponentDecorator extends Component{
    private Component component; //维持对抽象构件类型对象的引用
    public ComponentDecorator(Component component) //注入抽象构件类型的对象
    {
        this.component = component;
    }
    public void display()
    {
        component.display();
    }
}
//滚动条装饰类:具体装饰类
class ScrollBarDecorator extends ComponentDecorator
    {
    public ScrollBarDecorator(Component component)
    {
        super(component);
    }
    public void display()
    {
        this.setScrollBar();
        super.display();
    }
    public void setScrollBar()
    {
        System.out.println("为构件增加滚动条!");
    }
}
//黑色边框装饰类:具体装饰类
class BlackBorderDecorator extends ComponentDecorator{
    public BlackBorderDecorator(Component component)
    {
        super(component);
    }
    public void display()
    {
        this.setBlackBorder();
        super.display();
    }
    public void setBlackBorder()
    {
        System.out.println("为构件增加黑色边框!");
    }
}
编写如下客户端测试代码:
class Client{
    public static void main(String args[])
    {
        Component component,componentSB; //使用抽象构件定义
        component = new Window(); //定义具体构件
        componentSB = new ScrollBarDecorator(component); //定义装饰后的构件
        componentSB.display();
    }
}

如果我们
希望得到一个既有滚动条又有黑色边框的窗体,不需要对原有类库进行任何修改,只需将客
户端代码修改为如下所示:
class Client{
    public static void main(String args[]){
        Component component,componentSB,componentBB; //全部使用抽象构件定义
        component = new Window();
        componentSB = new ScrollBarDecorator(component);
        componentBB = new BlackBorderDecorator(componentSB); //将装饰了一次之后的对象继续注入到另一个装饰类中,进行第二次装饰
        componentBB.display();
    }
}




[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5z0374YF-1584001371016)(BD17BFDC59334398B9EA6F38F34D1136)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值