博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式之装饰者模式
阅读量:5094 次
发布时间:2019-06-13

本文共 3371 字,大约阅读时间需要 11 分钟。

装饰者模式

1.装饰者模式就像打包一个快递
(1)主体:陶瓷,衣服。
(2)报纸填充,塑料泡沫,纸板,木板
2.Component
3.ConcreteComponent和Decorator
4.装饰者模式:动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性。

5.有些递归的意思

类结构图

 

示例代码

 

package com.coffeebar.main;public abstract class Drink {	public String description = "";	private float price = 0f;	public String getDescription() {		return description;	}	public void setDescription(String description) {		this.description = description;	}	public float getPrice() {		return price;	}	public void setPrice(float price) {		this.price = price;	}		public abstract float cost();	}

 

 

package com.coffeebar.coffee;import com.coffeebar.main.Drink;public class Coffee extends Drink {	@Override	public float cost() {		// TODO Auto-generated method stub		return super.getPrice();	}}

 

package com.coffeebar.coffee;import com.coffeebar.main.Drink;public class Decaf extends Coffee {	public Decaf() {		super.setDescription("Decaf");		super.setPrice(3.0f);	}}

 

package com.coffeebar.coffee;public class Espresso extends Coffee {	public Espresso() {		super.setDescription("Espresso");		super.setPrice(4.0f);	}}

 

package com.coffeebar.coffee;public class LongBlack extends Coffee {	public LongBlack() {		super.setDescription("LongBlack");		super.setPrice(5.5f);	}}

 

package com.coffeebar.coffee;public class ShortBlack extends Coffee {	public ShortBlack() {		super.setDescription("ShortBlack");		super.setPrice(6.0f);	}}

 

package com.coffeebar.decorator;import com.coffeebar.main.Drink;public class Decorator extends Drink {	private Drink Obj;		public Decorator(Drink Obj) {		this.Obj = Obj;	}	@Override	public float cost() {		// TODO Auto-generated method stub		return super.getPrice() + Obj.cost();	}	public String Description() {		return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();	}}

 

package com.coffeebar.decorator;import com.coffeebar.main.Drink;public class Chocolate extends Decorator {	public Chocolate(Drink Obj) {		super(Obj);		// TODO Auto-generated constructor stub		super.setDescription("Chocolate");		super.setPrice(3.0f);	}	}

 

package com.coffeebar.decorator;import com.coffeebar.main.Drink;public class Milk extends Decorator {	public Milk(Drink Obj) {		super(Obj);		// TODO Auto-generated constructor stub		super.setDescription("Milk");		super.setPrice(2.0f);	}}

 

package com.coffeebar.decorator;import com.coffeebar.main.Drink;public class Soy extends Decorator {	public Soy(Drink Obj) {		super(Obj);		// TODO Auto-generated constructor stub		super.setDescription("Soy");		super.setPrice(1.5f);	}}

 

package com.coffeebar.main;import com.coffeebar.coffee.Decaf;import com.coffeebar.coffee.LongBlack;import com.coffeebar.decorator.Chocolate;import com.coffeebar.decorator.Milk;public class Coffeebar {	/**	 * @param args	 */	public static void main(String[] args) {		// TODO Auto-generated method stub		Drink order;		order = new Decaf();		System.out.println("order1 price: " + order.cost());		System.out.println("order1 desc: " +order.getDescription());				System.out.println("*******************");		order = new LongBlack();		order = new Milk(order);		order = new Chocolate(order);		order = new Chocolate(order);		System.out.println("order2 price: " + order.cost());		System.out.println("order2 desc: " + order.getDescription());	}}

 输出结果

order1 price: 3.0order1 desc: Decaf*******************order2 price: 13.5order2 desc: Chocolate

 

 

转载于:https://www.cnblogs.com/chengzi123/p/4781926.html

你可能感兴趣的文章
后台往前台写弹窗代码不显示
查看>>
Altium Designer 电源部分
查看>>
Hive读取外表数据时跳过文件行首和行尾
查看>>
C#入门
查看>>
spring-mybatis项目练习 - 通讯录系统,ssm配置文件部分
查看>>
Java判断数据库表是否存在的方法
查看>>
将博客搬至CSDN
查看>>
模型驱动设计的构造块
查看>>
screen窗口管理器
查看>>
Oracle总结之plsql编程(基础九)
查看>>
转:Java Annotation入门(二)(中英文结合)
查看>>
Python从入门到入土-Pycharm如何使用python3
查看>>
java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter
查看>>
TensorFlow+Keras 01 人工智能、机器学习、深度学习简介
查看>>
js控制页面显示
查看>>
Linux 学习手记(6): 磁盘、分区、MBR与GPT
查看>>
【ES6】var / let / const
查看>>
bootstrap-multiselect 的简单使用,样式修改,动态创建option
查看>>
【转载】malloc内存分配与free内存释放的原理
查看>>
ftp、ssh
查看>>