设计模式--装饰者模式

2020-09-03 16:04:56 浏览数 (2)

咖啡馆订单项目:

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

  2)、调料:Milk、Soy、Chocolate

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

一个差的方案:

一个好一点的设计方案

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

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

  1)主体:陶瓷、衣服

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

装饰者模式:

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

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

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

对应代码演示:

代码语言:javascript复制
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();
    
}
代码语言:javascript复制
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();
    }

    
}
代码语言:javascript复制
package com.java.jikexueyuan.coffeebar.coffee;

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

}
代码语言:javascript复制
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();
    }
    
    }
代码语言:javascript复制
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);
    }

}
代码语言:javascript复制
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);
    }

}

测试方法:

代码语言:javascript复制
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

代码语言:javascript复制
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;
    }
}
代码语言:javascript复制
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();
        }

    }
}

0 人点赞