• 设计模式 —— 装饰器模式(Decorator Pattern)


    装饰器模式(Decorator Pattern)

    概念

    装饰器模式 允许向一个现有的对象添加新的功能,同时又不改变其结构。装饰者可以在所委托被装饰者的行为之前或之后加上自己的行为,以达到特定的目的。

    组成

    装饰器模式

    装饰器模式由组件和装饰者组成。

    抽象组件(Component):需要装饰的抽象对象。 
    具体组件(ConcreteComponent):是我们需要装饰的对象 
    抽象装饰类(Decorator):内含指向抽象组件的引用及装饰者共有的方法。 
    具体装饰类(ConcreteDecorator):被装饰的对象。


    例子:

    假设我们现在去咖啡店要了一杯咖啡,可以加奶、加糖等等。咖啡和奶、糖分别有不同的价格。 
    咖啡就是我们的组件,奶和糖是我们的装饰者,现在我们要计算调制这样一杯咖啡花费多少。

    装饰器模式

    Drink 接口类:

    package DesignPattern.Strategy.Decorator;
    
    public interface Drink {
        public float cost();
        public String getDescription();
    }

    Coffee 类:

    package DesignPattern.Strategy.Decorator;
    
    public class Coffee implements Drink {
        final private String description = "coffee";
        //每杯 coffee 售价 10 元
        public float cost() {
            return 10;
        }
    
        public String getDescription() {
            return description;
        }
    }

    CondimentDecorator 调味抽象类:

    package DesignPattern.Strategy.Decorator;
    
    public abstract class CondimentDecorator implements Drink {
        protected Drink decoratorDrink;
    
        public CondimentDecorator(Drink decoratorDrink) {
            this.decoratorDrink = decoratorDrink;
        }
    
        public float cost() {
            return decoratorDrink.cost();
        }
    
        public String getDescription() {
            return decoratorDrink.getDescription();
        }
    }

    Milk 牛奶装饰类:

    package DesignPattern.Strategy.Decorator;
    
    public class Milk extends CondimentDecorator {
        public Milk(Drink decoratorDrink) {
            super(decoratorDrink);
        }
    
        @Override
        public float cost() {
            return super.cost() + 2;
        }
    
        @Override
        public String getDescription() {
            return super.getDescription() + " milk";
        }
    }

    Sugar 装饰类:

    package DesignPattern.Strategy.Decorator;
    
    public class Sugar extends CondimentDecorator {
        public Sugar(Drink decoratorDrink) {
            super(decoratorDrink);
        }
    
        @Override
        public float cost() {
            return super.cost() + 1;
        }
    
        @Override
        public String getDescription() {
            return super.getDescription() + " sugar";
        }
    }

    测试代码:

    package DesignPattern.Strategy.Decorator;
    
    public class CoffeeShop {
        public static void main(String[] args) {
            //点一杯coffee
            Drink drink = new Coffee();
            System.out.println(drink.getDescription() + ":" + drink.cost());
            //加一份奶
            drink = new Milk(drink);
            System.out.println(drink.getDescription() + ":" + drink.cost());
            //加一份糖
            drink = new Sugar(drink);
            System.out.println(drink.getDescription() + ":" + drink.cost());
            //再加一份糖
            drink = new Sugar(drink);
            System.out.println(drink.getDescription() + ":" + drink.cost());
        }
    }

    装饰器模式

    上图我们可以看出 coffee 加不同的调味料价格的不同。


    适用场景:

    • 扩展一个类的功能。
    • 动态增加功能,动态撤销。

    优缺点:

    优点:

    • 装饰类和被装饰类可以独立发展,不会相互耦合
    • 动态的将责任附加到对象身上。

    缺点:

    • 多层装饰比较复杂。

    参考:

    Head First 设计模式

     
  • 相关阅读:
    二十八 .Django中模型类中Meta元对象了解
    【POJ2376】Cleaning Shifts
    【CF600E】Lomsat gelral
    【hiho1035】自驾旅行III
    【模板】manachar
    【hiho1065】全图传送
    【洛谷P1450】硬币购物
    【HDU2204】Eddy's爱好
    【CF208E】Blood Cousins
    【CF451E】Devu and Flowers
  • 原文地址:https://www.cnblogs.com/shenbo-/p/9074032.html
Copyright © 2020-2023  润新知