Java装饰设计模式的例子
2013-10-28 12:44
681 查看
这里给出一个顾客购买咖啡的例子。其中咖啡可以加冰(2元),加巧克力(4元)。
下面是面向对象中装饰模式的解决方案。
下面是面向对象中装饰模式的解决方案。
/** * Created with IntelliJ IDEA. * User: HYY * Date: 13-10-27 * Time: 下午10:49 * To change this template use File | Settings | File Templates. */ interface Product { public double money(); } class Coffee implements Product{ @Override public double money() { return 10; } } class Ice implements Product { private Product product; public Ice(Product product) { this.product = product; } @Override public double money() { return this.product.money() + 2;//加冰两块钱 } } class Chocolate implements Product { private Product product; public Chocolate(Product product) { this.product = product; } @Override public double money() { return this.product.money() + 4;//加糖四块钱 } } public class BuyCoffee { public static void main(String[] args) { Product coffee = new Coffee(); Product iceCoffee = new Ice(coffee); Product chocolateIceCoffee = new Chocolate(iceCoffee); System.out.println("你买了巧克力加冰咖啡,一共:"+chocolateIceCoffee.money()+"元钱。"); } }
相关文章推荐
- java设计模式-装饰模式
- java设计模式-装饰模式
- Java设计模式12:装饰器模式
- Java设计模式学习09——装饰模式
- JAVA随笔——设计模式--装饰模式
- Java设计模式之装饰器模式
- JAVA设计模式之 装饰模式【Decorator Pattern】
- java IO 字节流、字节流读写文件、字节流缓冲区、文件拷贝、装饰设计模式
- Android 内功心法(1.9)——java设计模式之装饰模式
- (三)Java设计模式--装饰器模式
- JAVA设计模式-装饰设计模式-继承体系的由来和装饰类的优化
- Java 设计模式 - 装饰模式
- JAVA基础——装饰设计模式
- Java进阶篇设计模式之五-----外观模式和装饰器模式
- Java-马士兵设计模式学习笔记-观察者模式-AWT简单例子
- java学习之IO装饰设计模式
- JAVA IO之装饰器模式(Head first 设计模式之装饰器模式)
- 黑马程序员-JAVA基础-IO流中的装饰设计模式
- 浅析Java设计模式之装饰器模式
- java 设计模式之装饰模式