美文网首页
设计模式之装饰者模式

设计模式之装饰者模式

作者: 柴崎越 | 来源:发表于2019-02-22 21:35 被阅读0次

    一,项目需求

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

    二,类的关系

    装饰者模式.png

    三,代码实现

    1.Drink类

    此类是最顶级的父类

    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();
    } 
    

    以下是咖啡的种类

    1 coffee所有咖啡的父类(不含有调味剂)

    /**
     * Coffee的总称 
     * 价格交给子类设置
     */
    public class Coffee extends Drink{
        @Override
        public float cost() {
            return super.getPrice();
        }
    
    }
    

    1.1 Decaf

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

    1.2 Espresso

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

    1.3 LongBlack

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

    1.3 ShortBlack

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

    2.修饰类(调味)

    中间的一个父类

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

    2.1 Chocolate

    public Chocolate(Drink obj) {
           super(obj);
           super.setDescription("Chocolate");
           super.setPrice(3.0f);
       }
    

    2.2 Milk

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

    2.3 Soy

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

    3.测试类

    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());
            
        }
    
    运行结果: 运行结果.png

    四.重点代码分析

    @Override
        public float cost() {
            System.out.println(super.getPrice());
            return super.getPrice()+obj.cost();
        }
        
        @Override
        public String getDescription()
        {
            return super.description+"-"+super.getPrice()+"&&"+obj.getDescription();
        }
    
    实现图解如下图: 这是测试代码实现的步骤 递归调用.png 类的关系.png

    相关文章

      网友评论

          本文标题:设计模式之装饰者模式

          本文链接:https://www.haomeiwen.com/subject/jpeyyqtx.html