设计模式之装饰器模式

装饰器模式

组成

  • 抽象组件角色(Component): 定义可以动态添加任务的对象的接口
  • 具体组件角色(ConcreteComponent):定义一个要被装饰器装饰的对象,即 Component 的具体实现
  • 抽象装饰器(Decorator): 维护对组件对象和其子类组件的引用
  • 具体装饰器角色(ConcreteDecorator):向组件添加新的职责

结构图:

img

弱弱的说一下他的思路:

Component是定义一个对象接口,为对象动态的添加职责。ConcreteComponent是定义了一个具体对象,也可以给这个对象添加一些职责。

Decorator,装饰抽象类,继承了Components,从外类来扩展Component类的功能。但是对于Component来说。无需知道Decorator的存在的。至于ConcreteDecorator就是具体装饰对象,起到给Component添加职责的功能、

再深入变通一下,假如只有ConcreteComponent类没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同理,如果过只有一个ConcreteDecorator,就没必要单独建立一个Decorator,所以我们就可以把这个Decorator类和ConcreteDecorator类的合并成一个类了呀。这样直接让哦我们的服饰类Decorator类直接继承ConcreteComponent就可以了。

实现

Peson类

public class person {

    private String name;

    public person() {
    }

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

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

Decorator类

public class Finery  extends person{
    protected person component;
    /**
     * 打扮
     * @param component
     */
    public void decorate(person component){
        this.component = component;
    }

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

具体服饰类ConcreteDecorator

/**具体服饰类bigTrouser*/
public class bigTrouser extends Finery{
    @Override
    public void show(){
        System.out.print("垮裤\t");
        super.show();
    }
}
/**具体服饰类tshirts*/
public class tshirts extends Finery {
    @Override
    public void show(){
        System.out.print("大T恤\t");
        super.show();
    }
}

其他的太多了就不一一列举了。

客服端

/**
 * @author:pier 2021/11/15
 **/
/**客服端*/
public class Main {

    public static void main(String[] args) {

        person person = new person("Java_Pier~呀");

        System.out.println("第一种装扮");

        wearSneakers sneakers = new wearSneakers();
        bigTrouser bigTrouser = new bigTrouser();
        tshirts tShirts = new tshirts();

        sneakers.decorate(person);
        bigTrouser.decorate(sneakers);
        tShirts.decorate(bigTrouser);
        tShirts.show();

        System.out.println("第二种装扮");
        wearLeathershoes leatherShoes = new wearLeathershoes();
        wearTie tie = new wearTie();
        wearSuit suit = new wearSuit();
        leatherShoes.decorate(person);
        tie.decorate(leatherShoes);
        suit.decorate(tie);
        suit.show();

    }
}


实现结果

第一种装扮
大T恤	垮裤	破球鞋	装扮的Java_Pier~呀
第二种装扮
西装	领带	皮鞋	装扮的Java_Pier~呀

进程已结束,退出代码为 0

总结上述优点:

把类中的装饰功能去除,简化原有的类。

实现结果*

第一种装扮
大T恤	垮裤	破球鞋	装扮的Java_Pier~呀
第二种装扮
西装	领带	皮鞋	装扮的Java_Pier~呀

进程已结束,退出代码为 0

总结上述优点:

把类中的装饰功能去除,简化原有的类。

有效的那核心职责和装饰功能区分开。

参考:《大话设计模式》

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值