美文网首页
装饰者模式

装饰者模式

作者: Ethan_Walker | 来源:发表于2018-04-25 13:34 被阅读1次

原文链接

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

image.png

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

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

实例:

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

image.png

Drink 类 (抽象组件:Component)

public interface Drink {
    double getPrice();
    String getConsist();
}

Coffee 类 (具体组件:ConcreteComponent )

public class Coffee implements Drink {
    @Override
    public double getPrice() {
        return 10;
    }

    @Override
    public String getConsist() {
        return "coffee ";
    }
}

Decorator 类(抽象装饰类:Decorator)


public abstract class Decorator implements Drink {
    protected Drink drink;

    public Decorator(Drink drink) {
        this.drink = drink;
    }

    @Override
    public double getPrice() {
        return drink.getPrice();
    }

    @Override
    public String getConsist() {
        return drink.getConsist();
    }
}

MilkCoffee 类 (具体装饰类:ConcreteDecorator)


public class MilkCoffee extends Decorator {

    public MilkCoffee(Drink drink) {
        super(drink);
    }

    @Override
    public String getConsist() {
        String consist = super.getConsist();
        consist += " milk";
        return consist;

    }

    @Override
    public double getPrice() {
        double price = super.getPrice();
        price += 2.1;
        return price;
    }
}

SugarCoffee 类 (具体装饰类: ConcreteDecorator)

public class SugarCoffee extends Decorator{
    public SugarCoffee(Drink drink){
        super(drink);
    }

    @Override
    public double getPrice() {
        double price = super.getPrice();
        price+=0.5;
        return price;
    }

    @Override
    public String getConsist() {
        String consist = super.getConsist();
        consist+=" sugar";
        return consist;
    }
}

测试类

public class Demo {
    public static void main(String[] args) {
        Drink drink = new Coffee();
        System.out.println(drink.getConsist() + "==>" + drink.getPrice());

        drink = new MilkCoffee(drink);
        System.out.println(drink.getConsist() + "==>" + drink.getPrice());

        drink = new SugarCoffee(drink);
        System.out.println(drink.getConsist() + "==>" + drink.getPrice());

        drink = new SugarCoffee(drink);
        System.out.println(drink.getConsist() + "==>" + drink.getPrice());

    }
}

输出

coffee ==>10.0
coffee  milk==>12.1
coffee  milk sugar==>12.6
coffee  milk sugar sugar==>13.1

相关文章

  • 如何利用装饰者模式在不改变原有对象的基础上扩展功能

    目录 什么是装饰者模式 普通示例 装饰者模式示例 类图关系 装饰者模式使用场景 装饰者模式优点 装饰者模式缺点 什...

  • 装饰者模式

    装饰者模式 装饰者模式和适配器模式对比 装饰者模式 是一种特别的适配器模式 装饰者与被装饰者都要实现同一个接口,主...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式-装饰者模式

    装饰者模式概念: 装饰者模式又名包装(Wrapper)模式。装饰者模式以对客户端透明的方式扩展对象的功能,是继承关...

  • java - 装饰者模式

    装饰者模式 装饰者模式:动态将责任添加到对象上。如果需要扩展功能,装饰者提供了比继承更有弹性的解决方案。装饰者模式...

  • 设计模式之装饰者模式(Decorator Pattern)

    What: 装饰者模式又名包装(Wrapper)模式。装饰者模式动态地将责任附加到对象身上。若要扩展功能,装饰者提...

  • 装饰者(Decorator)模式

    装饰者(Decorator)模式装饰模式又名包装(Wrapper)模式。装饰模式是继承关系的一个替代方案。装饰模式...

  • 2、装饰者模式

    装饰者模式 一、基本概念 二、结构 三、案例1、装饰者模式案例2、JavaIO中使用装饰者模式 四、总结 一、基本...

  • PHP的设计模式-装饰者模式

    装饰者模式 装饰者模式 装饰者模式类似蛋糕,有草莓味、奶酪等种类,但是它们的核心都是蛋糕。 不断地将对象添加装饰的...

  • 设计模式 | 装饰者模式及典型应用

    前言 本文的主要内容: 介绍装饰者模式 示例 源码分析装饰者模式的典型应用Java I/O 中的装饰者模式spri...

网友评论

      本文标题:装饰者模式

      本文链接:https://www.haomeiwen.com/subject/uxaelftx.html