设计模式(二)之装饰器模式
来源:互联网 发布:马龙 知乎 编辑:程序博客网 时间:2024/05/21 03:28
定义一个代表被装饰事物的接口:
public interface Coffee { public String getCoffer(); public double getPrice();}
最初的具体事物:
public class Starbucks implements Coffee { @Override public String getCoffer() { return "星巴克"; } @Override public double getPrice() { return 10D; }}
被装饰后的具体事物:
public class Seasoning implements Coffee { private Coffee coffee; public Seasoning(Coffee coffee) { this.coffee = coffee; } @Override public String getCoffer() { return coffee.getCoffer(); } @Override public double getPrice() { return coffee.getPrice(); }}
一号装饰器:
public class SeasoningOne extends Seasoning implements Coffee { public SeasoningOne(Coffee coffee) { super(coffee); } @Override public String getCoffer() { return super.getCoffer() + " 糖"; } @Override public double getPrice() { return super.getPrice() + 1.0D; }}
二号装饰器:
public class SeasoningTwo extends Seasoning implements Coffee { public SeasoningTwo(Coffee coffee) { super(coffee); } @Override public String getCoffer() { return super.getCoffer() + " 牛奶"; } @Override public double getPrice() { return super.getPrice() + 5.0D; }}
三号装饰器:
public class SeasoningThree extends Seasoning implements Coffee { public SeasoningThree(Coffee coffee) { super(coffee); } @Override public String getCoffer() { return super.getCoffer() + " 摩卡"; } @Override public double getPrice() { return super.getPrice() + 3.0D; }}
使用者:
public class Consumer { public static void main(String[] args) { Coffee coffee = new SeasoningOne(new Seasoning(new Starbucks())); System.out.println(coffee.getCoffer() + ":" + coffee.getPrice()); coffee = new SeasoningTwo(new SeasoningOne(new Seasoning( new Starbucks()))); System.out.println(coffee.getCoffer() + ":" + coffee.getPrice()); coffee = new SeasoningThree(new SeasoningTwo(new SeasoningOne( new Seasoning(new Starbucks())))); System.out.println(coffee.getCoffer() + ":" + coffee.getPrice()); }}
0 0
- 设计模式(二)之装饰器模式
- 【设计模式】之装饰器模式(二)-PHP
- 设计模式(二):装饰器模式
- Java设计模式之二装饰模式
- 设计模式之二:装饰者模式
- 设计模式之二装饰者模式
- android设计模式之装饰模式(二)
- IOS设计模式之二(门面模式,装饰器模式)
- iOS设计模式之二(门面模式,装饰器模式)
- iOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- ios设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- IOS设计模式之二(门面模式,装饰器模式)
- 设计模式(二)装饰者模式
- Java中String类的使用
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror【zt】
- 【leetcode】Reverse Bits
- ios 对图片的处理
- MSVCRTD.lib(crtexe.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStart
- 设计模式(二)之装饰器模式
- Android 系统稳定性---OOM,ANR
- apache.poi包简单操作word文档
- org.springframework.orm.hibernate3.HibernateQueryException: XXX is not mapped解决方案
- sql 在not in 子查询有null值情况下经常出现的陷阱
- P2105 背单词
- jQuery Mobile学习教程系列文章推荐
- 将单链表就地逆置
- Handler详解