装饰者模式(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) 具体解释的更多相关文章

  1. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  2. 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法

    装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...

  3. C#设计模式——装饰者模式(Decorator Pattern)

    一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...

  4. 设计模式学习--装饰者模式(Decorator Pattern)

    概念: 装饰者模式(Decorator Pattern): 动态地将功能添加到对象,相比生成子类更灵活,更富有弹性. 解决方案: 装饰者模式的重点是对象的类型,装饰者对象必须有着相同的接口,也也就是有 ...

  5. 大话设计模式--装饰者模式 Decorator -- C++实现实例

    1.装饰者模式 Decorator 动态地给一个对象添加一个额外的职责, 就添加功能来说, 装饰模式比生成子类更为灵活. 每个装饰对象的实现和如何使用这个对象分离,  每个装饰对象只关心自己的功能,不 ...

  6. 23种设计模式之装饰器模式(Decorator Pattern)

    装饰器模式(Decorator Pattern) 允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰类,用来包 ...

  7. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  8. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  9. Android设计模式之中的一个个样例让你彻底明确装饰者模式(Decorator Pattern)

    导读 这篇文章中我不会使用概念性文字来说明装饰者模式.由于通常概念性的问题都非常抽象.非常难懂.使得读者非常难明确究竟为什么要使用这样的设计模式.我们设计模式的诞生,肯定是前辈们在设计程序的时候遇到了 ...

随机推荐

  1. android+eclipse+mysql+servlet(Android与mysql建立链接)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原创地址  .作者信息和本声明.http://www.cnblogs.com/zhu520/p/7724524.html 经过两天的时间我终于把A ...

  2. Problem B: 开个餐馆算算账

    Description 小明创业了!他开了一家餐馆,每天客人还挺多的.每天下班后,他都要算算今天总共收入多少钱,但是手工算太麻烦了,所以他来向你求助了. Input 第1行N>0,表示餐馆提供N ...

  3. jstree 获取选中节点的所有子子点

    //加载功能树 function initTree() { $.jstree.destroy(); $.ajax({ type: "Get", url: "/Depart ...

  4. HTML基础--position 绝对定位 相对定位 锚点链接

    position 定位属性,检索对象的定位方式 一.语法:position:static /absolute/relative/fixed 取值: 1.static:默认值,无特殊定位,对象遵循HTM ...

  5. 从零一起学Spring Boot之LayIM项目长成记(五)websocket

    前言 距离上一篇已经比较久的时间了,项目也是开了个头.并且,由于网上的关于Spring Boot的websocket讲解也比较多.于是我采用了另外的一个通讯框架 t-io 来实现LayIM中的通讯功能 ...

  6. x86平台上的Windows页表映射机制

    首先,在x86架构的处理器上,一个正常页面大小为4KB,非PAE模式下,CR3持有页目录页面的物理地址,PDE和PTE格式相同大小为4字节.此时每个页表页面包含1024个PTE,可以映射1024个页面 ...

  7. Microsoft SQL Server 2008 R2数据库备份 - 人工备份

    业务介绍 数据库人工备份是指由相关管理人员通过主动的手工方式备份数据库文件.在一些特殊的时间节点,如重要资料的录入完成.软硬件环境更新前等需要特别关注数据库安全的时候,一定要进行数据库的人工备份,以保 ...

  8. 《java.util.concurrent 包源码阅读》27 Phaser 第一部分

    Phaser是JDK7新添加的线程同步辅助类,作用同CyclicBarrier,CountDownLatch类似,但是使用起来更加灵活: 1. Parties是动态的. 2. Phaser支持树状结构 ...

  9. Linux上安装和卸载mysql数据库 (一)

    一.前言 第一次写博客,很激动同时有点畏惧,激动是我可以将我的经验进行分享,畏惧是我怕我写的东西,大家借鉴的时候,有些步骤不能成功.不过,我还是很有信息的,我分享的经验都是我搭建成功以后才分享出来.这 ...

  10. C语言之找零钱

    #include<stdio.h>int main(){ int one,tow,five,num=1; for (one = 1; one < num*10; one++) { f ...