举例说, 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!因为工作繁忙,所以不可能 ...
随机推荐
- 浅谈spring——注解配置(九)
spring定义一个切面是件麻烦的事情,需要实现专门的接口,还要进行一些较为复杂的配置,有没有较为简单的方法??? @AspectJ注解可以很容易定义一个切面,且不需要实现任何的接口.缺点是对JDK的 ...
- 关于jdbc注冊驱动的那点事
看到非常多人写jdbc连接工具类的时候,都会写到Class.forName()去显示载入类,一写错点点就会抛出ClassNotFoundException,关于显示载入类,究竟会不会产生作用呢? 參考 ...
- DOS批处理延时技术
DOS批处理延时技术 举个例子,我们要延时5秒打开gdh.txt这个文件,可以用以下几个方法 方法一:ping 缺点:时间精度为1秒,不够精确 www.2cto.com @echo off ...
- 你是否是团队里面最默默付出的那个coder,却发现滔滔不绝的产品和设计是团队里的开心果(转)
程序员,你是否是团队里面最默默付出的那个coder,却发现滔滔不绝的产品和设计是团队里的开心果? 你是否自命不凡,精通Java.C++.Python……却发现得到的只是做不完的工作? 你是否觉得自己是 ...
- fzu2150(bfs)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 题意:在任意两处点火,求最短时间烧光所有草堆. 分析:由于n,m比较小,将所有草堆坐标记录下来,然后暴力 ...
- NYOJ 12 喷水装置(二)
pid=12">喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n( ...
- SQL Server Compact免安装部署
原文:SQL Server Compact免安装部署 情况 应用程序中的EF使用了SQL Server Compact,打包部署到客户机器上后提示数据库连接异常,信息类似”配置节“.”Provider ...
- 利用SVNKit进行版本库的树的导出
public List searchByTree(String userName,String passwd,String SVNServerUrl,String dirUrl){ //这里有点像 s ...
- 【Cloud Foundry】Could Foundry学习(三)——Router
在阅读的过程中有不论什么问题.欢迎一起交流 邮箱:1494713801@qq.com QQ:1494713801 一.概述 Router组件在Cloud Foundry中是对全部进来的Reque ...
- 编译安装LNMP Centos 6.5 x64 + Nginx1.6.0 + PHP5.5.13 + Mysql5.6.19
(来自:http://www.cnblogs.com/vicowong/archive/2011/12/01/2116212.html) 环境: 系统硬件:vmware vsphere (CPU:2* ...