设计模式 - 装饰者模式(Decorator Pattern) 具体解释
装饰者模式(Decorator Pattern) 具体解释
本文地址: http://blog.csdn.net/caroline_wendy/article/details/26707033
装饰者模式(Decorator Pattern):动态地将责任附加到对象上. 若要扩展功能, 装饰者提供了比继承更有弹性的替代方案.
用法:
1. 首先创建组件(Component)父类, 全部类,详细组件(Concrete Component)和装饰者(Decorator)都属于这一类型,
能够进行扩展;
能够是抽象类(abstract class), 也能够是接口(interface);
代码:
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public abstract class Beverage {
- String description = "Unkown Beverage";
- public String getDescription() {
- return description;
- }
- public abstract double cost();
- }
2. 装饰者(Decorator)类父类, 继承组件(component)父类, 可是不要实现接口函数, 由它的继承类(详细的装饰者)去实现,
能够是抽象类(abstract class),
也能够是接口(interface);
代码:
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public abstract class CondimentDecorator extends Beverage {
- public abstract String getDescription();
- }
3. 详细组件(Concrete Component), 即装饰者(Decorator)须要装饰的基础, 继承组件(Component)父类;
代码:
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class DarkRoast extends Beverage {
- public DarkRoast() {
- description = "Dark Roast";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 0.99;
- }
- }
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class Decat extends Beverage {
- public Decat() {
- description = "Decat";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 1.05;
- }
- }
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class Espresso extends Beverage {
- public Espresso() {
- description = "Espresso";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 1.99;
- }
- }
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class HouseBlend extends Beverage {
- public HouseBlend() {
- description = "House Blend Coffee";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return .89;
- }
- }
4. 装饰者(Decorator)类继承装饰者父类, 实现组件父类的接口和装饰者父类的接口.
代码:
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class Milk extends CondimentDecorator {
- Beverage beverage;
- public Milk(Beverage beverage) {
- this.beverage = beverage;
- }
- /* (non-Javadoc)
- * @see decorator.CondimentDecorator#getDescription()
- */
- @Override
- public String getDescription() {
- // TODO Auto-generated method stub
- return beverage.getDescription() + ", Milk";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 0.10 + beverage.cost();
- }
- }
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class Mocha extends CondimentDecorator {
- Beverage beverage;
- public Mocha(Beverage beverage) {
- this.beverage = beverage;
- }
- /* (non-Javadoc)
- * @see decorator.CondimentDecorator#getDescription()
- */
- @Override
- public String getDescription() {
- // TODO Auto-generated method stub
- return beverage.getDescription() + ", Mocha";
- }
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 0.20+beverage.cost();
- }
- }
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class Soy extends CondimentDecorator {
- Beverage beverage;
- public Soy(Beverage beverage) {
- this.beverage = beverage;
- }
- /* (non-Javadoc)
- * @see decorator.CondimentDecorator#getDescription()
- */
- @Override
- public String getDescription() {
- // TODO Auto-generated method stub
- return beverage.getDescription() + ", Soy";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 0.15 + beverage.cost();
- }
- }
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class Whip extends CondimentDecorator {
- Beverage beverage;
- public Whip(Beverage beverage) {
- this.beverage = beverage;
- }
- /* (non-Javadoc)
- * @see decorator.CondimentDecorator#getDescription()
- */
- @Override
- public String getDescription() {
- // TODO Auto-generated method stub
- return beverage.getDescription() + ", Whip";
- }
- /* (non-Javadoc)
- * @see decorator.Beverage#cost()
- */
- @Override
- public double cost() {
- // TODO Auto-generated method stub
- return 0.10 + beverage.cost();
- }
- }
5. 測试, 创建详细组件(Concrete Component), 再一层一层加入装饰者(Decorator)类, 能够实现动态的组合;
代码:
- /**
- * @time 2014年5月23日
- */
- package decorator;
- /**
- * @author C.L.Wang
- *
- */
- public class StarbuzzCoffee {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Beverage beverage = new Espresso();
- System.out.println(beverage.getDescription() +
- " $" + beverage.cost());
- Beverage beverage2 = new DarkRoast();
- beverage2 = new Mocha(beverage2);
- beverage2 = new Mocha(beverage2);
- beverage2 = new Whip(beverage2);
- System.out.println(beverage2.getDescription() +
- " $" + beverage2.cost());
- Beverage beverage3 = new HouseBlend();
- beverage3 = new Soy(beverage3);
- beverage3 = new Mocha(beverage3);
- beverage3 = new Whip(beverage3);
- System.out.println(beverage3.getDescription() +
- " $" + beverage3.cost());
- }
- }
6. 输出:
- Espresso $1.99
- Dark Roast, Mocha, Mocha, Whip $1.49
- House Blend Coffee, Soy, Mocha, Whip $1.34
面向对象的原则:
对扩展开发, 对改动关闭.
设计模式 - 装饰者模式(Decorator Pattern) 具体解释的更多相关文章
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- C#设计模式——装饰者模式(Decorator Pattern)
一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...
- 设计模式学习--装饰者模式(Decorator Pattern)
概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...
- 大话设计模式--装饰者模式 Decorator -- C++实现实例
1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离, 每个装饰对象只关心自己的功能,不 ...
- 23种设计模式之装饰器模式(Decorator Pattern)
装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)
导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...
随机推荐
- ASP.NET MVC中URL末尾斜杠的实现
在网站的SEO优化中,通常都会涉及到URL结尾斜杠的问题. http://blog.sina.com.cn/s/blog_828e7ce40100srj1.html http://www.dengyo ...
- Android开发之漫漫长途 Ⅴ——Activity的显示之ViewRootImpl的PreMeasure、WindowLayout、EndMeasure、Layout、Draw
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- canvas画布实现手写签名效果
最近项目中涉及到移动端手写签名的功能需求,将实现代码记录于此,供小伙伴们参考指摘哦~ HTML代码: <!--手写区--> <div class="mSign_signMa ...
- 创建并在项目中调用SQLSERVER存储过程的简单示例
使用SQLSERVER存储过程可以很大的提高程序运行速度,简化编程维护难度,现已得到广泛应用.创建存储过程 和数据表一样,在使用之前需要创建存储过程,它的简明语法是: 引用: Create PROC ...
- ES6中class关键字
1 .介绍 //定义类 class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + th ...
- pt-query-digest
pt-query-digest默认查询时间分布 # Query_time distribution # 1us # 10us ##################################### ...
- iOS 远程推送消息解析及逻辑处理
关于远程推送的相关配置网上已经有足够多的教程,这里就不复述了.这里讲述当客户端收到推送消息后,应怎样对其进行相应的逻辑处理. 工程的AppDelegate.m文件里提供了如下方法: //当应用程序启动 ...
- C# 处理Word自动生成报告 二、数据源例子
还是以学生.语文.数学.分数为例吧, 感觉这个和helloworld都有一拼了. 造一张表如下, 整张报表就围绕这个表转圈了, 顺便说下就是名字如有雷同纯属巧合 新建个存储过程 ALTER PROCE ...
- css中的float和position
1.float <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- VS2015远程调试
原文链接 VS2015远程调试 在PayPal支付时,PayPal回调函数一直报错,本地没有外网IP,没有办法在本地调试,需要远程调试: 1.找到远程调试的文件夹: 找到对应的服务器的型号:64位 ...