当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 大话设计模式笔记(四)の装饰模式

大话设计模式笔记(四)の装饰模式

2019年06月24日  | 移动技术网IT编程  | 我要评论

举个栗子

问题描述

可以给人搭配嘻哈服或白领装的程序。

简单实现

代码

/**
 * 人类
 * created by callmedevil on 2019/6/23.
 */
public class person {

    private string name;

    public person(string name) {
        this.name = name;
    }

    public void weartshirts(){
        system.out.print("大t恤 ");
    }

    public void wearbigtrouser(){
        system.out.print("垮裤 ");
    }

    public void wearsneakers(){
        system.out.print("破球鞋 ");
    }

    public void wearsuit(){
        system.out.print("西装 ");
    }

    public void weartie(){
        system.out.print("领带 ");
    }

    public void wearleathershoes(){
        system.out.print("皮鞋 ");
    }

    public void show(){
        system.out.println("装扮的" + name);
    }

}
/**
 * 装扮测试类
 * created by callmedevil on 2019/6/23.
 */
public class test {

    public static void main(string[] args) {
        person devil = new person("devil");

        system.out.println("第一种装扮:");
        devil.weartshirts();
        devil.wearbigtrouser();
        devil.wearsneakers();
        devil.show();

        system.out.println("\n第二种装扮:");
        devil.wearsuit();
        devil.weartie();
        devil.wearleathershoes();
        devil.show();
    }

}

测试结果

第一种装扮:
大t恤 垮裤 破球鞋 装扮的devil

第二种装扮:
西装 领带 皮鞋 装扮的devil

存在缺陷

如果需要增加“超人”装扮,会导致需要修改“person”类,违背了开放-封闭原则

简单实现进化版

代码

/**
 * 人类
 * created by callmedevil on 2019/6/23.
 */
public class person {
    
    private string name;

    public person(string name){
        this.name = name;
    }

    public void show(){
        system.out.print("装扮的" + name);
    }

}
/**
 * 服装抽象类
 * created by callmedevil on 2019/6/23.
 */
public abstract class finery {
    
    public abstract void show();
    
}
/**
 * t恤 类
 * created by callmedevil on 2019/6/23.
 */
public class tshirts extends finery {
    
    @override
    public void show() {
        system.out.print("大t恤 ");
    }
    
}
/**
 * 垮裤 类
 * created by callmedevil on 2019/6/23.
 */
public class bigtrouser extends finery {

    @override
    public void show() {
        system.out.print("垮裤 ");
    }

}

// 其余子类相似,此处省略
/**
 * 装饰升级版测试
 * created by callmedevil on 2019/6/23.
 */
public class test {

    public static void main(string[] args) {
        person devil = new person("devil");

        system.out.println("第一种装扮:");
        finery tshirts = new tshirts();
        finery bigtrouser = new bigtrouser();
        finery sneakers = new sneakers();
        tshirts.show();
        bigtrouser.show();
        sneakers.show();
        devil.show();

        system.out.println("\n第二种装扮:");
        finery suit = new suit();
        finery tie = new tie();
        finery leathershoes = new leathershoes();
        suit.show();
        tie.show();
        leathershoes.show();
        devil.show();
    }

}

测试结果

第一种装扮:
大t恤 垮裤 破球鞋  装扮的devil

第二种装扮:
西装 领带 皮鞋 装扮的devil

存在问题

现在如果要加超人装扮,只要增加子类就可以了,但是这么做虽然把“服装”类和“人”类分离开了,仍然是存在问题的。把“大t恤”、“垮裤”、“破球鞋”和“装扮的devil”一个词一个词显示出来,就好比:你光着身子,当着大家的面,先穿t恤,再穿裤子,再穿鞋,仿佛在跳穿衣舞。。。因此需要一个房间(组合类)来换衣服,同时这个穿的顺序对每个人来说是不固定的,有的人喜欢先穿裤子,再穿鞋,最后穿t恤。。只需要把所需的功能按正确的顺序串联起来进行控制即可。

装饰模式

定义

动态地给一个对象添加一些额外的职责,就增加来说,装饰模式比生成子类更为灵活

uml图

  • component是定义一个对象接口,可以给这些对象动态地添加职责。
  • concretecomponent是定义了一个具体的对象,也可以给这个对象添加一些职责。
  • decorator,装饰抽象类,继承了component,从外类来扩展component类功能,但对于component来说,是无需知道decorator的存在的。
  • 至于concretedecorator就是具体的装饰对象,起到了给component添加职责的功能。

简单实现究极进化->装饰模式实现

代码

/**
 * 究极进化人类(concretecomponent)
 * created by callmedevil on 2019/6/23.
 */
public class person {

    private string name;

    public person(){}

    public person(string name){
        this.name = name;
    }

    public void show(){
        system.out.println("装扮的" + name);
    }

}
/**
 * 究极进化 服饰类(decorator)
 * created by callmedevil on 2019/6/23.
 */
public abstract class finery extends person{

    protected person component;

    /**
     * 装扮
     * @param component
     */
    public void decorate(person component) {
        this.component = component;
    }

    @override
    public void show() {
        if (component != null) {
            component.show();
        }
    }

}
/**
 * 究极进化 t恤(concretedecorator)
 * created by callmedevil on 2019/6/23.
 */
public class tshirts extends finery{

    @override
    public void show() {
        system.out.print("大t恤 ");
        super.show();
    }

}
/**
 * 究极进化 垮裤(concretedecorator)
 * created by callmedevil on 2019/6/23.
 */
public class bigtrouser extends finery {

    @override
    public void show() {
        system.out.print("垮裤 ");
        super.show();
    }

}

// 其余子类相似,此处省略
/**
 * 装饰模式测试
 * created by callmedevil on 2019/6/23.
 */
public class test {

    public static void main(string[] args) {
        person devil = new person("devil");

        system.out.println("第一种装扮:");
        sneakers sneakers = new sneakers();
        bigtrouser bigtrouser = new bigtrouser();
        tshirts tshirts = new tshirts();
        // 装饰
        sneakers.decorate(devil);
        bigtrouser.decorate(sneakers);
        tshirts.decorate(bigtrouser);
        tshirts.show();

        system.out.println("\n第二种装扮:");
        leathershoes leathershoes = new leathershoes();
        tie tie = new tie();
        suit suit = new suit();
        // 装饰
        leathershoes.decorate(devil);
        tie.decorate(leathershoes);
        suit.decorate(tie);
        suit.show();
    }

}

测试结果

第一种装扮:
大t恤 垮裤 破球鞋  装扮的devil

第二种装扮:
西装 领带 皮鞋 装扮的devil

总结

  • 装饰模式是利用setcomponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中。
  • 如果只有一个concretecomponent类而没有抽象的component类,那么decorator类可以是concretecomponent的一个子类。同样道理,如果只有一个concretedecorator类,那么就没有必要建立一个单独的decorator类,而可以把decoratorconcretedecorator的责任合并成一个类。
  • 装饰模式是为已有功能动态地添加更多功能的一种方式
  • 不使用装饰模式,且系统需要新功能的时候,是向旧的类中添加新的代码,这些新加的代码通常装饰了原有类的核心职责或主要行为。在主类中加入了新的字段、方法和逻辑,会增加主类的复杂度,而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊需求。装饰模式提供了非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象。
  • 装饰模式的优点是把类中装饰功能从类中搬移去除,这样可以简化原有的类。这么做最大的好处就是有效地把类的核心职责和装饰功能区分开了,而且可以去除相关类中重复的装饰逻辑。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网