Java设计模式(3)-装饰者模式

2018-09-15  本文已影响6人  小白201808

一. 学习项目:咖啡馆订单系统

  咖啡馆订单:
  1)咖啡种类:Espresso,ShortBlack,LongBlack,Decaf
  2)调料:Milk,Soy,Chocolate
  3)扩展性好,改动方便,维护方便

1.一个差的解决方案:

       饮品超类:
      (1)<abstract>Drink
           description
           getDescription()
           cost()
           
       咖啡单品:
       (1) Decaf
           cost()
           
       (2) ShortBlack
           cost()              
      (3) LongBlack
            cost()
       (4) Espresso
            cost()
        
       添加调料咖啡:
       (1)Espresso && Milk
            cost()
            
       (3)Espresso  && Soy
            cost()
                 
       (3)Espresso && Milk && Soy
            cost()       
            
         ...   
         ...
   拓展性很差,到时可能遇见类爆炸。。。

2. 一个较好的设计方案

饮品超类:
    <abstract>Drink
        desciprtion
        milk
        soy
        chocolate
        
     getDescription()
     cost()
     hasMilk()
     setMilk()
     hasSoy()
     setSoy()
     hasChocolate()
     setChocolate()
 
  咖啡单品:
       (1) Decaf
           cost()
           
       (2) ShortBlack
           cost()              
      (3) LongBlack
            cost()
       (4) Espresso
            cost()
  
 存在一些问题:
   (1)增删调料种类
   (2)添加多份问题 

3.使用装饰者模式

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

设计方案:

1.
<abstract>Drink
    description
    getDescription()
    cost()
  
2.
Decorator
   Drink obj
   getDescription()
   cost()
   ...

3.
    
     咖啡单品:
     (1) Decaf
         cost()
         
     (2) ShortBlack
         cost()              
    (3) LongBlack
          cost()
     (4) Espresso
          cost()
          
     调料单品:
     (1)Chocolate
           getDescription()
           cost()
     (2)Milk
           getDescription()
           cost()
     (3)Soy
           getDescription()
           cost()               
    

代码实现:

(1) Drink

package com.java.keen.coffeebar;
//饮品类:描述,价格
public abstract class Drink {
    public String description = "";
    private float price = 0f;
    
    public String getDescription() {
        return description+"  "+this.getPrice();
    }
    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();
}

(2)Decorator

package com.java.keen.coffeebar.decorator;

import com.java.keen.coffeebar.Drink;

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

(3)Coffee

package com.java.keen.coffeebar.coffee;

import com.java.keen.coffeebar.Drink;

public class Coffee extends Drink {

    @Override
    public float cost() {
        
        return super.getPrice();
    }
    

}

(4)各种咖啡单品

//1.Decaf
package com.java.keen.coffeebar.coffee;

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

}


//2.Espresso
package com.java.keen.coffeebar.coffee;

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

}

//3.LongBlack
package com.java.keen.coffeebar.coffee;

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

}

//4.ShortBlack 
package com.java.keen.coffeebar.coffee;

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

}

(5)各种调料单品:

//1.Chocolate
package com.java.keen.coffeebar.decorator;

import com.java.keen.coffeebar.Drink;

public class Chocolate extends Decorator{

    public Chocolate(Drink obj) {
        super(obj);
        // TODO Auto-generated constructor stub
        super.setDescription("Chocolate");
        super.setPrice(3.0f);
    }

}


//2.Milk
package com.java.keen.coffeebar.decorator;

import com.java.keen.coffeebar.Drink;

public class Milk extends Decorator{

    public Milk(Drink obj) {
        super(obj);
        // TODO Auto-generated constructor stub
        super.setDescription("milk");
        super.setPrice(2.5f);
    }

}



//3.Soy
package com.java.keen.coffeebar.decorator;

import com.java.keen.coffeebar.Drink;

public class Soy extends Decorator{

    public Soy(Drink obj) {
        super(obj);
        // TODO Auto-generated constructor stub
        super.setDescription("soy");
        super.setPrice(1.5f);
    }

}

(6)测试类

package com.java.keen.coffeebar;

import com.java.keen.coffeebar.coffee.*;
import com.java.keen.coffeebar.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);
         order =new Chocolate(order);
         System.out.println("order2 price:  " +order.cost());
         System.out.println("order2 desc :  "+order.getDescription());
    
    
    }
}

output:

order1 price:  3.0
order1 desc :  Decaf  3.0
***************
order2 price:  13.5
order2 desc :  Chocolate+3.0&&Chocolate+3.0&&milk+2.5&&LongBlack  5.0

个人在极客学院的学习的笔记

上一篇下一篇

猜你喜欢

热点阅读