编程语言
首页 > 编程语言> > 设计模式(13):JAVA(13):设计模式(6)装饰器模式:允许向一个现有的对象添加新的功能,同时又不改变其结构

设计模式(13):JAVA(13):设计模式(6)装饰器模式:允许向一个现有的对象添加新的功能,同时又不改变其结构

作者:互联网

例1

package com.woniuxy.m_decorator.a;

/*
  星巴克是卖咖啡的, 一开始饮料种类比较少,所以使用继承,看不出什么问题。

  每种咖啡,都有描述,都有价格。 这是所有咖啡的共性,既然是共性,就要上提到父类中。
 
*/

abstract class Beverage {
    
    private String description;

    public Beverage(String description) {
        super();
        this.description = description;
    }
    
    public abstract double cost(); 
}


class DarkRoast extends Beverage {

    public DarkRoast(String descriptoin) {
        super(descriptoin);
    }
    
    public double cost() {
        return 10;
    }
}

class Decaf extends Beverage {

    public Decaf(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return 8;
    }
}

class Espresso extends Beverage {

    public Espresso(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return 12;
    }
    
}

class HouseBlend extends Beverage {

    public HouseBlend(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return 15;
    }
    
}

public class Test {

}

例2

星巴克,业务壮大了,为了把同业抛开,它加了新的花样: 4种调料:

蒸奶(steamed Milk)、豆浆(Soy)、摩卡(Mocha)、打起奶泡(Whip)

 

针对于DrakRoast咖啡,可以有以下几种变化:
DrakRoast
DrakRoastWithSteamedMilk
DrakRoastWithSoy
DrakRoastWithMocha
DrakRoastWithWhip

DrakRoastWithSteamedMilkAndSoy

.....

类,爆炸了!! 程序员,疯了!!

 

 

例3

package com.woniuxy.m_decorator.c;


// 为了解决b包的问题,
// 作者,尝试这样来解决:


// 优点,没有类爆炸!

 class Beverage {
    
    private String description;
    
    private boolean steamedMilk;
    private boolean soy;
    private boolean mocha;
    private boolean whip;

    public Beverage(String description) {
        super();
        this.description = description;
    }
    public  double cost() {
        
        double cost = 0;
        
        if(steamedMilk) {
            cost += 0.2;
        }
        if(soy) {
            cost += 0.2;
        }
        if(mocha) {
            cost += 0.3;
        }
        if(whip) {
            cost += 0.1;
        }
        
        return cost;
    }
    
    /*
     public  double cost(Map map) {
         double cost = 0;
         for(Entry e : map.entrySet() ) {
             cost += e.getValue();
         }
         return cost;
     }
     
     public String description(Map map) {
             String str = 0;
             for(Entry e : map.entrySet() ) {
                 str += e.getKey() + " ";
             }
             return str;
     }
     
     
     Map map = new HashMap();
     map.put("Milk", 0.2);
     map.put("Soy", 0.2);
     map.put("Whip", 0.1);
     map.put("GouqI", 0.1);
     
     cost(map);
     
     getDescription(map);
     
     
    */
    
    
    public boolean isSteamedMilk() {
        return steamedMilk;
    }
    public void setSteamedMilk(boolean steamedMilk) {
        this.steamedMilk = steamedMilk;
    }
    public boolean isSoy() {
        return soy;
    }
    public void setSoy(boolean soy) {
        this.soy = soy;
    }
    public boolean isMocha() {
        return mocha;
    }
    public void setMocha(boolean mocha) {
        this.mocha = mocha;
    }
    public boolean isWhip() {
        return whip;
    }
    public void setWhip(boolean whip) {
        this.whip = whip;
    }
    
    
    
}


class DarkRoast extends Beverage {

    public DarkRoast(String descriptoin) {
        super(descriptoin);
    }
    
    public double cost() {
        return super.cost() + 10;
    }
    
    /*
     
     public String getDescription(Map map) {
         return descriptoin + super.getDesciption(Map map);
     }
     
     */
}

class Decaf extends Beverage {

    public Decaf(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return super.cost() + 8;
    }
    
}

class Espresso extends Beverage {

    public Espresso(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return super.cost() + 12;
    }
    
}

class HouseBlend extends Beverage {

    public HouseBlend(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return super.cost() + 15;
    }
    
}

// =========================================================

class Tea extends Beverage {

    public Tea(String description) {
        super(description);
    }
    
    public double cost() {
        return super.cost() + 9;
    }
    
}

public class Test {

    public static void main(String[] args) {
        DarkRoast dr = new DarkRoast("焦炒咖啡");
        dr.setMocha(true);
        dr.setSoy(true);
        System.out.println(dr.cost());
        
        HouseBlend hb = new HouseBlend("混合咖啡");
        hb.setWhip(true);
        System.out.println(hb.cost());
        
        
        Tea tea = new Tea("凉茶");
        tea.setSoy(true);
        System.out.println(tea.cost());
    }

}

// 缺点:
// 现在多了一个调料:枸杞。

例4

package com.woniuxy.m_decorator.d;

import java.io.IOException;

// 为了解决c包的问题,

abstract class Beverage {
    
    private String description;

    public Beverage(String description) {
        super();
        this.description = description;
    }
    public abstract double cost();
    
    public String getDescription() {
        return description;
    }
 }


class DarkRoast extends Beverage {

    public DarkRoast(String descriptoin) {
        super(descriptoin);
    }
    
    public double cost() {
        return  10;
    }
}

class Decaf extends Beverage {

    public Decaf(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return  8;
    }
}

class Espresso extends Beverage {

    public Espresso(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return 12;
    }
    
}

class HouseBlend extends Beverage {

    public HouseBlend(String description) {
        super(description);
    }

    @Override
    public double cost() {
        return  15;
    }
    
}


abstract class CondimentDecorator extends Beverage {

    public CondimentDecorator() {
        super("调料");
    }
    
    public abstract double cost();
    
}

class Milk extends CondimentDecorator {

    private Beverage beverage;
    
    public Milk(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.2;
    }
    
    @Override
    public String getDescription() {
        return beverage.getDescription() + " 牛奶";
    }
    
}

class Mocha extends CondimentDecorator{
    private Beverage beverage;
    public Mocha(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.3;
    }
    
    @Override
    public String getDescription() {
        return beverage.getDescription() + " 摩卡";
    }
    
}

class Soy extends CondimentDecorator {
    private Beverage beverage;
    public Soy(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.2;
    }
    
    @Override
    public String getDescription() {
        return beverage.getDescription() + " 豆浆";
    }
    
}

class Whip extends CondimentDecorator {
    private Beverage beverage;
    public Whip(Beverage beverage) {
        this.beverage = beverage;
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.1;
    }
    
    @Override
    public String getDescription() {
        return beverage.getDescription() + " 奶泡";
    }
    
}

// =========================================================


class GouQi extends CondimentDecorator {

    private Beverage beverage;
    
    public GouQi(Beverage beverage) {
        super();
        this.beverage = beverage;
    }
    
    @Override
    public String getDescription() {
        return beverage.getDescription() + " 枸杞";
    }

    @Override
    public double cost() {
        return beverage.cost() + 0.4;
    }
    
}

public class Test {

    public static void main(String[] args) throws IOException {
        
        Beverage b = new DarkRoast("焦炒咖啡");
        
        Beverage b2 = new Milk(b);
        
        Beverage b3 = new Soy(b2);
        
        Beverage b4 = new Whip(b3);
        
        Beverage b5 = new Milk(b4);
        
        Beverage b6 = new GouQi(b5);
        
        System.out.println(b6.getDescription() + ":" +  b6.cost());
        
        
    }
}

 

例5

package com.woniuxy.m_decorator.e;

import java.io.FileReader;
import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;

// 根据已有功能,做出更强大的功能!
class MyBufferedReader extends FilterReader {

    private Reader in;
    
    protected MyBufferedReader(Reader in) {
        super(in);
        this.in = in;
    }
    
    public String readLine() throws IOException {
        
        StringBuilder sb = new StringBuilder("");
        int n = 0;
        while((n = in.read()) != -1) {
            if(n == '\r' ) {
                continue;
            }
            if(n == '\n' ) {
                break;
            }
            sb.append((char)n);
        }
        
        // 流程能走到这里,n有几种可能? 一个是-1,  是\n
        return n == -1  ? null: sb.toString();
        
    }
    
    @Override
    public void close() throws IOException {
        in.close();
    }
    
    
    
}

public class Test {
    public static void main(String[] args) throws Exception {
        Reader in = new FileReader("F:\\1.txt");
        MyBufferedReader mbr = new MyBufferedReader(in);
        
        String line = null;
        
        while((line = mbr.readLine()) != null) {
            System.out.println(line);
        }
        mbr.close();
        
    }
}

 

标签:13,JAVA,description,String,Beverage,return,设计模式,public,cost
来源: https://www.cnblogs.com/qiu-hua/p/15227406.html