举例说, 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!因为工作繁忙,所以不可能 ...
随机推荐
- OCP读书笔记(5) - 使用RMAN创建备份
5.Creating Backups with RMAN 创建备份集 RMAN> backup as backupset format '/u01/app/oracle/backup/rmanb ...
- ExtJS学习-------Ext正确Dom操作:Ext.get Ext.fly Ext.getDom
详细实例: (1)创建JSP文件.引入CSS和js文件,加入三个Div <%@ page language="java" import="java.util.*&q ...
- 伤不起的戴尔台式机XPS8700脆弱的蓝牙
http://en.community.dell.com/support-forums/desktop/f/3514/t/19520747.aspx 1.报价仅仅包含主机,并且不带音响(speaker ...
- 你不知道的JavaScript上卷笔记
你不知道的JavaScript上卷笔记 前言 You don't know JavaScript是github上一个系列文章 初看到这一标题的时候,感觉怎么老外也搞标题党,用这种冲突性比较强的题目 ...
- AngularJS是为了克服HTML在构建应用上的不足而设计的
AngularJS中文网:http://www.apjs.net/ 简介 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构 ...
- android中更改spinner、AutoCompleteTextView切割线的颜色
话说去除切割线的方法找了非常久也没找到,最终发现了更改切割线的方法 spinner和AutoCompleteTextView提示列表中间有一条黑色的切割线.想要改变它的颜色值,就要重写style. 1 ...
- SE 2014年4月16日
一. 描述BGP路由协议中 BGP路由携带 AS-PATH/ next-hop / ORIGIN / local-preference 属性的特点! BGP协议中的AS-PATH是AS列表,用来 ...
- 《Linux命令行与shell脚本编程大全》 第十六章 学习笔记
第十六章:创建函数 基本的脚本函数 创建函数 1.用function关键字,后面跟函数名 function name { commands } 2.函数名后面跟空圆括号,标明正在定义一个函数 name ...
- Keepalived+LVS+Nginx负载均衡之高可用
Keepalived+LVS+Nginx负载均衡之高可用 上一篇写了nginx负载均衡,此篇实现高可用(HA).系统整体设计是采用Nginx做负载均衡,若出现Nginx单机故障,则导致整个系统无法正常 ...
- ocx控件手动修改clsid的方法
替换掉工程的两个地方:IDL文件和CTRL文件. IMPLEMENT_OLECREATE_EX(CMultiwndCtrl, "MULTIWND.MultiwndCtrl.1", ...