Product.java
- public interface Product {
-
- public int money();
- }
public interface Product {
public int money();
}
Coffee.java
- public class Coffee implements Product {
-
- public int money() {
- return 10;
- }
-
- }
public class Coffee implements Product {
public int money() {
return 10;
}
}
Sugar.java
- public class Sugar implements Product {
-
- private Product p;
-
- public Sugar(Product p){
- this.p = p;
- }
-
- public int money() {
- return p.money()+5;
- }
-
- }
public class Sugar implements Product {
private Product p; //组合
public Sugar(Product p){
this.p = p;
}
public int money() {
return p.money()+5;
}
}
Milk.java
- public class Milk implements Product {
-
- private Product p;
-
- public Milk(Product p){
- this.p = p;
- }
- public int money() {
- return p.money()+10;
- }
-
- }
public class Milk implements Product {
private Product p;
public Milk(Product p){
this.p = p;
}
public int money() {
return p.money()+10;
}
}
Main.java
- public class Main {
-
- public static void main(String[] args) {
-
-
- Coffee coffee = new Coffee();
- System.out.println(coffee.money());
-
-
- Sugar s = new Sugar(coffee);
- System.out.println(s.money());
-
-
- Milk m = new Milk(coffee);
- System.out.println(m.money());
-
-
- Milk milk = new Milk(new Sugar(new Coffee()));
- System.out.println(milk.money());
- }
-
- }
分享到:
相关推荐
设计模式--装饰者模式java例子
装饰者模式Demo
装饰者模式 C++ 实现
设计模式 - 装饰者模式
装饰者模式代码demo 装饰者模式代码demo 装饰者模式代码demo
c# ,设计模式,装饰者模式,星巴兹咖啡,基本上就是head first 上的例子的C#实现,根据自己的理解,作了一点点改动,可以直接运行。
装饰者模式的典型应用。 装饰者模式的典型应用。 装饰者模式的典型应用。
Qt设计模式之装饰者模式
设计模式的装饰者模式的例子,希望对大家有用~~~~~~~~
装饰者模式demo
java装饰者模式实现,通过豆浆,加糖加鸡蛋加黑豆生动的实现了装饰着的代码,可以学习,也可以在此基础上进行扩充
装饰者模式学习案例代码 文章:http://blog.csdn.net/hailushijie/article/details/8630106
Head First 设计模式 (三) 装饰者模式(decorator pattern) C++实现 VS2012 下通过
设计模式中的装饰模式,AndroidStudio工程
java设计模式【之】装饰者模式【源码】【场景:煎饼果子+肠+蛋】 * 测试类【之】煎饼果子来一套 * * 不改变原有对象的基础上,强化已经存在的功能 * 被装饰者与装饰者实现同一个抽象或接口 * 装饰后,最终还是...
本章可以称为“给爱用继承的人一个全新的设计眼界”。我们即将再度探讨典型...一旦你熟悉了装饰者的技巧,你将能够在不修改任何底层代码的情况下,给你的(或别人的)对象赋予新的职责。——《Head First 设计模式》
装饰者模式结合模板方法模式实现奶茶商店:原来的奶茶商店需要引进咖啡,用装饰者模式结合模板方法模式
java设计模式之装饰者模式代码