前言

很久没有写关于设计模式的博客了,实在是没有太多的精力去写。但个人觉得设计模式在我们的日常开发中还是挺重要的,它提高了软件的可维护性。因此还是有必要坚持学习设计模式,写博客主要是为了加深我对设计模式的理解。今天我要讲的设计模式是装饰者模式(Dectorator),它是结构型模式的一员。如果有什么讲的不正确的地方,希望各位大佬指正。

思考题

首先,让我们思考下面的问题:

有这么一家奶茶店,希望开发一个计算奶茶价格的软件,当客户点一杯奶茶,并且加入某几样配料时,需要及时的计算出这杯奶茶的价格,下面是奶茶和配料的价格。
原味奶茶:10
珍珠:2
椰果:3
巧克力:5 例子:如果用户点椰果奶茶,那么他的价格就是 原味奶茶+椰果=13。

当没有学习过装饰者模式时,我会给出下面的解决思路:

Ingredient.java:

public interface Ingredient {
Integer price();
}

配料接口:所有的配料都要实现这个接口,该接口有一个价格方法。

Chocolate.java:

public class Chocolate implements Ingredient {
public Integer price() {
return 5;
}
}

Coconut.java:

public class Coconut implements Ingredient {
public Integer price() {
return 3;
}
}

Pearl.java:

public class Pearl implements Ingredient {
public Integer price() {
return 2;
}
}

以上的上我的配料的实现类,他们都实现了 Ingredient 接口,并且实现了 price 方法。

MilkTea.java:

import java.util.List;
import java.util.ArrayList; public class MilkTea {
private List<Ingredient> ingredientList = new ArrayList<>(); public void addIngredient(Ingredient ingredient) {
ingredientList.add(ingredient);
} public Integer countPrice() {
Integer allPrice = 10;
for (Ingredient ingredient : ingredientList) {
allPrice += ingredient.price();
}
return allPrice;
}
}

以上是奶茶类的实现,里面有一个 ingredientList 成员变量,使用 addIngredient 就可以增加配料,调用 countPrice 计算奶茶的价格。

TestMain.java:

public class TestMain {
public static void main(String... args) {
MilkTea milkTea = new MilkTea();
milkTea.addIngredient(new Chocolate());
System.out.println("巧克力奶茶:" + milkTea.countPrice()); MilkTea milkTea_1 = new MilkTea();
milkTea_1.addIngredient(new Coconut());
milkTea_1.addIngredient(new Pearl());
System.out.println("珍珠椰果奶茶:" + milkTea_1.countPrice());
}
}

下面给出该实现的uml类图。

装饰者设计模式

定义:动态的给特定对象赋予新的功能.

类图:

从上面的类图我们可以总结出以下几点:

1.实现装饰者模式,我们需要有一个公共接口,我们的装饰者和被装饰者都需要继承这个接口.

2.为了更好地维护代码,上面将被装饰者的公共的代码提取到了父类中,子类通过继承这个父类可以很容易的实现不同的特性.

3.在父类的接口中实现了 Material 接口,以保证装饰者可以被其他装饰者装饰.

4.父类中有成员变量 Material ,以保证每个装饰者都知道自己装饰的是什么对象.

重构思考题

Material.java:

public interface Material {
Integer price();
}

MilkTea.java:

public class MilkTea implements Material {
@Override
public Integer price() {
return 10;
}
}

Ingredient.java:

public abstract class Ingredient implements Material {
private Material material; public Ingredient(Material material) {
this.material = material;
} @Override
public Integer price() {
return material.price() + getPrice();
} public abstract Integer getPrice();
}

Chocolate.java:

public class Chocolate extends Ingredient {
public Chocolate(Material material) {
super(material);
} @Override
public Integer getPrice() {
return 5;
}
}

Coconut.java:

public class Coconut extends Ingredient {
public Coconut(Material material) {
super(material);
}
@Override
public Integer getPrice() {
return 3;
}
}

Pearl.java:

public class Pearl extends Ingredient {
public Pearl(Material material) {
super(material);
} @Override
public Integer getPrice() {
return 2;
}
}

MainTest.java:

public class MainTest {
public static void main(String... args) {
Material milkTea = new Chocolate(new MilkTea());
System.out.println("巧克力奶茶:" + milkTea.price()); Material milkTea_1 = new Coconut(new Pearl(new MilkTea()));
System.out.println("珍珠椰果奶茶:" + milkTea_1.price());
}
}

[design pattern](3) Dectorator的更多相关文章

  1. 说说设计模式~大话目录(Design Pattern)

    回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...

  2. 设计模式(Design Pattern)系列之.NET专题

    最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...

  3. [转]Design Pattern Interview Questions - Part 4

    Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...

  4. [转]Design Pattern Interview Questions - Part 2

    Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...

  5. [转]Design Pattern Interview Questions - Part 3

    State, Stratergy, Visitor Adapter and fly weight design pattern from interview perspective. (I) Can ...

  6. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  7. design pattern

    1. visitor design pattern http://butunclebob.com/ArticleS.UncleBob.IuseVisitor

  8. Design Pattern: Observer Pattern

    1. Brief 一直对Observer Pattern和Pub/Sub Pattern有所混淆,下面打算通过这两篇Blog来梳理这两种模式.若有纰漏请大家指正. 2. Use Case 首先我们来面 ...

  9. Scalaz(10)- Monad:就是一种函数式编程模式-a design pattern

    Monad typeclass不是一种类型,而是一种程序设计模式(design pattern),是泛函编程中最重要的编程概念,因而很多行内人把FP又称为Monadic Programming.这其中 ...

随机推荐

  1. 再谈PG索引-存储架构

    1.索引的基本架构 PG的索引是B+树,B+树是为磁盘或其他直接存取辅助设备而设计的一种平衡查找树,在B+树中,所有记录节点都是按键值的大小顺序存放在同一层的叶节点中,各叶节点指针进行连接: meta ...

  2. MySQL 用 limit 为什么会影响性能?

    一,前言 首先说明一下MySQL的版本: mysql> select version();+-----------+| version() |+-----------+| 5.7.17 |+-- ...

  3. CF 631B 题解

    题面 注意到每次只染色一行或者一列,那么我们最后输出第i行第j列的数字是多少的时候只需要看一下最后一次i行和第j行被染了什么颜色,所以我们需要对每一行和一列记录最后一次染色的颜色. 但是我们也需要比较 ...

  4. AT2294 Eternal Average

    题目 题目给我们的这个东西可以转化为一棵\(k\)叉树,有\(n+m\)个叶子节点,其中\(m\)个权值为\(1\),\(n\)个权值为\(0\),每个非叶子节点的权值为其儿子的平均值,现在问你根节点 ...

  5. HNUSTOJ-1621 Picking Cabbage(状态压缩DP)

    1621: Picking Cabbage 时间限制: 2 Sec  内存限制: 32 MB提交: 26  解决: 14[提交][状态][讨论版] 题目描述 Once, Doraemon and  N ...

  6. 如何减少程序间的耦合度?_DI与接口

    spring 开发提倡接口编程,配合DI技术可以更好的减少层(程序)与层(程序)之间的解耦合例子说明:  任务:要求:        1.打印机依赖纸张和墨盒        2.纸张有A4和B5两种  ...

  7. java实现spark常用算子之collect

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  8. 依据系统语言、设备、url 重定向对应页面

    1. 思路 获取浏览器语言.页面名称.区分手机端与电脑 根据特定方式命名 html 文件,然后独立文件,重定向 eg: - root -  gap.html     gap -    index.ht ...

  9. shell统计mysql当前连接数

    [root@push-- scripts]# mysql -S /var/lib/mysql//mysql.sock -uroot -phlsms_push_Zaq1xsw@ -e "sho ...

  10. Java Script入门

    学习来源:https://www.runoob.com/js/js-tutorial.html JavaScript 教程 JavaScript 是 Web 的编程语言. 所有现代的 HTML 页面都 ...