举例说, Decorator模式(Decorator Pattern)
前言
官方定义
场景
举例
装饰者模式最核心的部分,就是从下图開始
package com.klpchan.example.cafe;
public abstract class Cafe {
public String getDescription() {
return description;
}
public abstract float getPrice();
String description = "This is Cafe";
}
package com.klpchan.example.cafe;
public abstract class Decorator extends Cafe{
public Decorator(Cafe _cafe) {
// TODO Auto-generated constructor stub
this.cafe = _cafe;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return cafe.getDescription();
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return cafe.getPrice();
}
Cafe cafe;
}
package com.klpchan.example.cafe;
public class DeCaf extends Cafe{
public DeCaf() {
// TODO Auto-generated constructor stub
description = "This is DECAF cofe";
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return Constants.CAFE_DECAF_PRICE;
}
}
package com.klpchan.example.cafe;
public class Espresso extends Cafe{
public Espresso() {
// TODO Auto-generated constructor stub
description = "This is Espresso " ;
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return Constants.CAFE_ESPRESSO_PRICE;
}
}
package com.klpchan.example.cafe;
public class Constants {
//脱脂和意式两种咖啡的基本价格
public static final float CAFE_DECAF_PRICE = 8;
public static final float CAFE_ESPRESSO_PRICE = 9;
//摩卡、牛奶、巧克力三种口味调料的价格
public static final float DECORATOR_MOCHA_PRICE = 0.5f;
public static final float DECORATOR_MILK_PRICE = 0.4f;
public static final float DECORATOR_CHOCOLATE_PRICE = 0.8f;
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2xwY2hhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">
加入了三个详细的配料类摩卡、奶油和巧克力,源代码例如以下
package com.klpchan.example.cafe;
public class MochaDecorator extends Decorator{
public MochaDecorator(Cafe _cafe) {
super(_cafe);
// TODO Auto-generated constructor stub
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return super.getDescription() + " add mocha ";
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return super.getPrice() + Constants.DECORATOR_MOCHA_PRICE;
}
}
package com.klpchan.example.cafe;
public class MilkDecorator extends Decorator{
public MilkDecorator(Cafe _cafe) {
super(_cafe);
// TODO Auto-generated constructor stub
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return super.getDescription() + " add milk ";
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return super.getPrice() + Constants.DECORATOR_MILK_PRICE;
}
}
package com.klpchan.example.cafe;
public class ChocolateDecorator extends Decorator{
public ChocolateDecorator(Cafe _cafe) {
super(_cafe);
// TODO Auto-generated constructor stub
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return super.getDescription() + " add chocolate ";
}
@Override
public float getPrice() {
// TODO Auto-generated method stub
return super.getPrice() + Constants.DECORATOR_CHOCOLATE_PRICE;
}
}
Cafe cafe = new DeCaf();
Cafe mochaChocCafe = new MochaDecorator(new ChocolateDecorator(cafe));
System.out.println(mochaChocCafe.getDescription() + " Price is " + mochaChocCafe.getPrice()); Cafe cafe2 = new Espresso();
Cafe milkChocMochaCafe = new MochaDecorator(new ChocolateDecorator(new MilkDecorator(cafe2)));
System.out.println(milkChocMochaCafe.getDescription() + " Price is " + milkChocMochaCafe.getPrice());
适用性
结构
效果
我们能够使用一个或多个装饰者来装饰一个对象,装饰后的对象仍然是component对象。
和其他模式的关系
应用场景
收尾
版权声明:本文博主原创文章,博客,未经同意不得转载。
举例说, Decorator模式(Decorator Pattern)的更多相关文章
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033 装饰者 ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- python 设计模式之装饰器模式 Decorator Pattern
#写在前面 已经有一个礼拜多没写博客了,因为沉醉在了<妙味>这部小说里,里面讲的是一个厨师苏秒的故事.现实中大部分人不会有她的天分.我喜欢她的性格:总是想着去解决问题,好像从来没有怨天尤人 ...
- Java设计模式(7)装饰模式(Decorator模式)
Decorator常被翻译成"装饰",我觉得翻译成"油漆工"更形象点,油漆工(decorator)是用来刷油漆的,那么被刷油漆的对象我们称decoratee.这 ...
- 来杯咖啡-装饰者模式(Decorator)
前言 上篇[观察者模式]发布已经近一个月了,个人感觉反应并不太理想,因为大家响应都不是很积极,不知是文章那里写得有问题,而且也没有人提出过有价值的改进建议,多少感觉有些失望L!因为工作繁忙,所以不可能 ...
随机推荐
- 非对称加密RSA、Elgamal、背包算法、Rabin、D-H、ECC(椭圆曲线加密算法)等。使用最广泛的是RSA算法
非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey).公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密:如果用私 ...
- Do you master on array in C ?
Do you master on array in C ? 因为新标准C99的支持变长数组, 差点儿C的标准特性就是看着gcc来的(Linux 内核严重依赖GCC) int mani() { cons ...
- 基于WEB 的认证防火墙的设计
项目要求: 1. 采用Linux iptbles作为接入防火墙,默认放行所以访问入口的80端口 2. 访问者通过http://x.x.x.x 访问防火墙的认证系统,进行账号的登陆操作,同时系统对用 ...
- Django URL 命名空间
https://docs.djangoproject.com/en/1.5/topics/http/urls/#introduction 译文: URL 命名空间 简介: 当你需要部署一个应用的多个实 ...
- Matlab实现PCA
在主成分分析(PCA)中,介绍了PCA的数学原理,其有用Matlab能够非常方便地对矩阵进行操作! 比方,用Matlab求多个样本的协方差矩阵.求矩阵的特征根和特征向量等. 以下介绍用Matlab实现 ...
- Struts2 开发环境搭建
一.开发环境 eclipse+tomcat+struts-2.2.3 eclipse下载地址:http://www.eclipse.org/downloads/ tomca ...
- ubuntu下ssh使用 与 SCP 使用
1 ssh远程登录服务器 ssh username@remote_ip #将username换成自己的用户名,将remote_ip换成远程服务器的ip地址 2 将文件/文件夹从远程服务器拷至本地(sc ...
- JSTL自定义标签库 (二)
要定义自己的标签,首先写个java类,extends TagSupport 或者 implements Tag ,然后在类体里实现自己想要的方法,或者覆盖父类的方法. 我定义的MyTag代码如下: ...
- 字符串匹配的KMP算法(转)
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- •当对自己的技术足够自信的时候,拼的就是RP和面试技巧了(转)
前言 三月真是一个忙碌的季节,刚刚开学就需要准备各种面试和笔试(鄙视).幸运的是,在长达一个月的面试内推季之后,终于同时拿到了阿里和腾讯的offer,还是挺开心的.突而想起久未更新的博客,就冒昧学一学 ...