码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式--装饰者模式

时间:2018-09-06 00:06:55      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:auto   import   abstract   模式   自定义   咖啡   ring   bar   --   

咖啡馆订单项目:

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

  2)、调料:Milk、Soy、Chocolate

  3)、扩展性好、改动方便、维护方便

一个差的方案:

技术分享图片

一个好一点的设计方案

技术分享图片

有些问题 :   1)增删调料种类   2)添加多份问题

 

装饰者模式就像打包一个快递

  1)主体:陶瓷、衣服

  2)包装:报纸填充、塑料泡沫、纸板、木板

装饰者模式:

技术分享图片

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

用装饰者模式重新设计的方案

技术分享图片

装饰者模式下的订单:2份巧克力+一份牛奶的LongBlack

技术分享图片

 

对应代码演示:

package com.java.jikexueyuan.coffeebar;

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();
    
}
package com.java.jikexueyuan.coffeebar.coffee;

import com.java.jikexueyuan.coffeebar.Drink;

public  class Coffee extends Drink {//单品

    @Override
    public float cost() {
        // TODO Auto-generated method stub
        return super.getPrice();
    }

    
}
package com.java.jikexueyuan.coffeebar.coffee;

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

}
package com.java.jikexueyuan.coffeebar.decorator;

import com.java.jikexueyuan.coffeebar.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();
    }

    @Override
    public String getDescription()
    {
        return super.description+"-"+super.getPrice()+"&&"+Obj.getDescription();
    }
    
    }
package com.java.jikexueyuan.coffeebar.decorator;

import com.java.jikexueyuan.coffeebar.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.java.jikexueyuan.coffeebar.decorator;

import com.java.jikexueyuan.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);
    }

}

测试方法:

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

Java里装饰者介绍

技术分享图片

 

扩展自定义IO

package com.java.jikexueyuan.myiodecorator;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class UpperCaseInputStream extends FilterInputStream{

    protected UpperCaseInputStream(InputStream in) {
        super(in);
        // TODO Auto-generated constructor stub
    }

    public int read() throws IOException
    {
        int c=super.read();
        return c==-1?c:Character.toUpperCase((char)(c));
    }
    public int read(byte[] b,int offset,int len) throws IOException
    {
        int result=super.read(b,offset,len);
        for(int i=0;i<result;i++)
        {
            b[i]=(byte)Character.toUpperCase((char)(b[i]));
        }
        
        return result;
    }
}
package com.java.jikexueyuan.myiodecorator;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputTest {
    public static void main(String[] args) {
        int c;
        try {
            InputStream in = new UpperCaseInputStream(new BufferedInputStream(
                    new FileInputStream("F:\\test.txt")));
            while((c=in.read())>=0)
            {
                System.out.print((char)c);
                
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

 

设计模式--装饰者模式

标签:auto   import   abstract   模式   自定义   咖啡   ring   bar   --   

原文地址:https://www.cnblogs.com/lm970585581/p/9594921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!