举例说, 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!因为工作繁忙,所以不可能 ...
随机推荐
- 关于JAVA Project.waitfor()死锁问题
在正常情况下我们可以用Project.waitfor()的返回值是否等于0的方法来判断java调用外部程序是Pass或者是Fail. 但是这个方法往往会被因进程堵塞而导致程序发生死锁,无法再继续执行外 ...
- docs/pcs/rest/file data apis list - 百度开发者中心
docs/pcs/rest/file data apis list - 百度开发者中心 更新通知: 2013.6.20 上传.下载新域名正式上线使用,相关接口“上传单个文件”.“分片上传-文件分片上传 ...
- stm32f103 TIM1初始化--定时器应用
//TIM1 分频 #define TIM1_DIV1 (1-1) #define TIM1_DIV2 (2-1) #define TIM1_DIV4 (4-1) #define TIM1_DIV8 ...
- nRF Toolbox 1.2 使用AKII的实现,而Becon始终不好使
这几天调试使用nRF51822驱动mpu6050及其数据传输到android中,调试的过程遇到一些困难,apptimer不太会用,然后就參考了下ble_app_hrs的程序,结果成功搞定,demo的价 ...
- hdu2063+hdu1083(最大匹配数)
传送门:hdu2063过山车 #include <cstdio> #include <cstring> #include <string> #include < ...
- Mars之android的Handler(2)
handler .looper.messageque的关系在前面已经有个介绍,但前面handler(1)中handler的使用是极少的一种情况,因为handler.sendMessage()可以在Ma ...
- Error : APP-FND-01926: The custom event WHEN-LOGON-CHANGED raised unhandled exception: ORA-06502: PL
In this Document _afrLoop=440418974213449&id=1508865.1&_afrWindowMode=0&_adf.ctrl-stat ...
- WCF之添加自定义用户名密码认证
1.创建WCF服务应用以及调用客户端(请自行google). 2.创建X509证书 cmd 进入 C:\Program Files\Microsoft SDKs\Windows\v6. ...
- Get与Post的差别
Http定义了与server交互的不同方法,最主要的方法有4种,各自是GET,POST.PUT,DELETE. URL全称是资源描写叙述符.我们能够这样觉得:一个URL地址,它用于描写叙述一个网络上的 ...
- 关于oracle的备份 导入
****假设要保存为bat文件.最好用汉字 导入: imp clsoftoa/clsoftoa@orcl124 --要导入的数据库的 username/password@数据库名 fromuser= ...