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

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

装饰者模式:动态的将新功能附加到对象上,在对象功能扩展方面,它比继承更有弹性。

一、咖啡馆订单项目

1)、咖啡种类 : Espresso、ShortBlack、LongBlack、Decaf

2)、调料 : Milk、Soy、Chocolate
3)、扩展性好、改动方便、维护方便

下面这个方案会类爆炸:

在这里插入图片描述

二、装饰者模式

在这里插入图片描述

1、Drink抽象类

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

2、Coffee类及其实现类

1)、Coffee基类

public  class Coffee extends Drink {	@Override	public float cost() {		return super.getPrice();	}}

2)、Decaf类

public class Decaf extends Coffee {	public Decaf()	{		super.setDescription("Decaf");		super.setPrice(3.0f);	}}

3)、Espresso类

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

4)、LongBlack类

public class LongBlack extends Coffee{	public LongBlack(){		super.setDescription("LongBlack");		super.setPrice(6.0f);	}}

3、Decorator类及其实现类

1)、Decorator基类

public  class Decorator extends Drink {	private Drink Obj;    		public Decorator(Drink Obj){		this.Obj=Obj;	};		@Override	public float cost() {				return super.getPrice()+Obj.cost();	}	@Override	public String getDescription(){		return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();	}	}

2)、Chocolate类

public class Chocolate extends Decorator {	public Chocolate(Drink Obj) {				super(Obj);		super.setDescription("Chocolate");		super.setPrice(3.0f);	}}

3)、Milk类

public class Milk extends Decorator {	public Milk(Drink Obj) {				super(Obj);		super.setDescription("Milk");		super.setPrice(2.0f);	}}

4、Decorator类及其实现类

public class CoffeeBar {	public static void main(String[] args) {				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);   //   一杯加Chocolate加牛奶的黑咖啡		order=new Chocolate(order);  //    一杯加两份Chocolate加牛奶的黑咖啡		System.out.println("order2 price:"+order.cost());		System.out.println("order2 desc:"+order.getDescription());		}}

三、java中装饰者模式

你可能感兴趣的文章
WAV文件解析
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>
QT打开项目提示no valid settings file could be found
查看>>
android 代码实现圆角
查看>>
java LinkedList与ArrayList迭代器遍历和for遍历对比
查看>>
drat中构造方法
查看>>
JavaScript的一些基础-数据类型
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>
Visual Studio 2010:C++0x新特性
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
Encoding Schemes
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Java8 HashMap集合解析
查看>>
自定义 select 下拉框 多选插件
查看>>