问题:
设计咖啡馆订单项目:
1)、咖啡种类:Espresso、ShortBlack、LongBlack、Decaf
2)、调料:Milk、Soy、Chocolate
3)、扩展性好、改动方便、维护方便
1、一个差的方案
2、 一个好一点的设计方案
3、 有些问题
1)增删调料种类
2)添加多份问题
装饰者模式原理
1、装饰者模式就像打包一个快递
1)主体:陶瓷、衣服
2)包装:报纸填充、塑料泡沫、纸板、木板
2、Component:
3、ConcreteComponent和Decorator:
4、 装饰者模式 : 动态的将新功能附加到对象上 。 在对象功能扩展方面 , 它
比继承更有弹性
新的项目设计方案
1、用装饰者模式设计重新设计的方案
2、装饰者模式下的订单:2份巧克力+一份牛奶的LongBlack
代码实现:
AbstractDrink
package com.java.jikexueyuan.coffeebar;
/**
*
*/
public abstract class AbstractDrink {
//描述信息
public String description="";
//价格
private float price=0f;;
//setter getter
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();
}
CoffeeDrink 继承AbstractDrink
package com.java.jikexueyuan.coffeebar.coffee;
import com.java.jikexueyuan.coffeebar.AbstractDrink;
public class CoffeeDrink extends AbstractDrink {
/**
*
* @return 花费的价格
*/
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice();
}
}
DecafCoffeeDrink 继承CoffeeDrink
package com.java.jikexueyuan.coffeebar.coffee;
public class DecafCoffeeDrink extends CoffeeDrink {
public DecafCoffeeDrink()
{
//设置描述信息
super.setDescription("DecafCoffeeDrink");
//设置价格
super.setPrice(3.0f);
}
}
EspressoCoffeeDrink继承CoffeeDrink
package com.java.jikexueyuan.coffeebar.coffee;
public class EspressoCoffeeDrink extends CoffeeDrink {
public EspressoCoffeeDrink()
{
//设置描述信息
super.setDescription("EspressoCoffeeDrink");
//设置价格
super.setPrice(4.0f);
}
}
LongBlackCoffeeDrink 继承 CoffeeDrink
package com.java.jikexueyuan.coffeebar.coffee;
public class LongBlackCoffeeDrink extends CoffeeDrink {
public LongBlackCoffeeDrink()
{
//设置描述信息
super.setDescription("LongBlackCoffeeDrink");
//设置价格
super.setPrice(6.0f);
}
}
ShortBlackCoffeeDrink继承 CoffeeDrink
package com.java.jikexueyuan.coffeebar.coffee;
public class ShortBlackCoffeeDrink extends CoffeeDrink {
public ShortBlackCoffeeDrink()
{
//设置描述信息
super.setDescription("ShortBlackCoffeeDrink");
//设置价格
super.setPrice(5.0f);
}
}
装饰者
Decorator 继承 AbstractDrink
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.AbstractDrink;
public class Decorator extends AbstractDrink {
//抽象类的引用
private AbstractDrink Obj;
public Decorator(AbstractDrink Obj){
this.Obj=Obj;
};
/**
* @return 价格
*/
@Override
public float cost() {
// TODO Auto-generated method stub
return super.getPrice()+Obj.cost();
}
/**
* @return 描述信息
*/
@Override
public String getDescription()
{
return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();
}
}
ChocolateDecorator 继承 Decorator
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.AbstractDrink;
public class ChocolateDecorator extends Decorator {
public ChocolateDecorator(AbstractDrink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("ChocolateDecorator");
super.setPrice(3.0f);
}
}
MilkDecorator 继承 Decorator
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.AbstractDrink;
public class MilkDecorator extends Decorator {
public MilkDecorator(AbstractDrink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("MilkDecorator");
super.setPrice(2.0f);
}
}
SoyDecorator继承 Decorator
package com.java.jikexueyuan.coffeebar.decorator;
import com.java.jikexueyuan.coffeebar.AbstractDrink;
public class SoyDecorator extends Decorator {
public SoyDecorator(AbstractDrink Obj) {
super(Obj);
// TODO Auto-generated constructor stub
super.setDescription("SoyDecorator");
super.setPrice(1.5f);
}
}
Test
package com.java.jikexueyuan.coffeebar;
import com.java.jikexueyuan.coffeebar.coffee.DecafCoffeeDrink;
import com.java.jikexueyuan.coffeebar.coffee.LongBlackCoffeeDrink;
import com.java.jikexueyuan.coffeebar.decorator.ChocolateDecorator;
import com.java.jikexueyuan.coffeebar.decorator.MilkDecorator;
public class CoffeeBar {
public static void main(String[] args) {
AbstractDrink order;
order=new DecafCoffeeDrink();
System.out.println("order1 price:"+order.cost());//3
System.out.println("order1 desc:"+order.getDescription());
System.out.println("****************");
order=new LongBlackCoffeeDrink();//6
order=new MilkDecorator(order);//6+2
order=new ChocolateDecorator(order);//6+2+3
order=new ChocolateDecorator(order);//6+2+3+3
System.out.println("order2 price:"+order.cost());
System.out.println("order2 desc:"+order.getDescription());
}
}
结果