《HeadFirst设计模式(第二版)》第三章代码——装饰者模式

代码文件结构:

《HeadFirst设计模式(第二版)》第三章代码——装饰者模式_第1张图片

 星巴兹案例:

CondimentDecorator
package Chapter3_DecorativeObjects.Decorators;

import Chapter3_DecorativeObjects.Beverage;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public abstract class CondimentDecorator extends Beverage {
    Beverage beverage;
    public abstract String getDescription();

    public Size getSize(){
        return this.beverage.getSize();
    }
}
Mocha
package Chapter3_DecorativeObjects.Decorators;

import Chapter3_DecorativeObjects.Beverage;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class Mocha extends CondimentDecorator{

    public Mocha(Beverage beverage){
        //在前一种饮料的基础上加上装饰
        //然后就可以通过 this.beverage.cost()来获取前者的花费,递归获取最终花费
        this.beverage = beverage;
    }

    @Override
    public String getDescription() {
        return this.beverage.getDescription()+", Mocha";//摩卡
    }

    @Override
    public double cost() {
        //递归获取最终花费
        double cost = this.beverage.cost();
        if(this.beverage.getSize() == Size.TALL){
            cost+=0.15;
        }else if(this.beverage.getSize() == Size.GRANDE){
            cost+=0.20;
        }else if(this.beverage.getSize() == Size.VENTI){
            cost+=0.25;
        }
        return cost;
    }
}
Soy
package Chapter3_DecorativeObjects.Decorators;

import Chapter3_DecorativeObjects.Beverage;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class Soy extends CondimentDecorator{

    public Soy(Beverage beverage){
        this.beverage = beverage;
    }

    @Override
    public String getDescription() {
        return this.beverage.getDescription()+", Soy";//豆奶
    }

    @Override
    public double cost() {
        //递归获取最终花费
        double cost = this.beverage.cost();
        if(this.beverage.getSize() == Size.TALL){
            cost+=0.10;
        }else if(this.beverage.getSize() == Size.GRANDE){
            cost+=0.15;
        }else if(this.beverage.getSize() == Size.VENTI){
            cost+=0.20;
        }
        return cost;
    }
}
Whip
package Chapter3_DecorativeObjects.Decorators;

import Chapter3_DecorativeObjects.Beverage;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class Whip extends  CondimentDecorator{

    public Whip(Beverage beverage){
        this.beverage = beverage;
    }

    @Override
    public String getDescription() {
        return this.beverage.getDescription()+", Whip";//奶泡
    }

    @Override
    public double cost() {
        //递归获取最终花费
        double cost = this.beverage.cost();
        if(this.beverage.getSize() == Size.TALL){
            cost+=0.08;
        }else if(this.beverage.getSize() == Size.GRANDE){
            cost+=0.10;
        }else if(this.beverage.getSize() == Size.VENTI){
            cost+=0.12;
        }
        return cost;
    }
}
Beverage
package Chapter3_DecorativeObjects;


/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public abstract class Beverage {
    String description = "Unknown Beverage";
    //加入小杯、中杯和大杯(tall, grande, venti)
    public enum Size{TALL, GRANDE, VENTI};

    Size size = Size.TALL;

    public Size getSize() {
        return size;
    }

    public void setSize(Size size) {
        this.size = size;
    }

    public String getDescription(){
        return this.description;
    }

    public abstract double cost();
}
Espresso
package Chapter3_DecorativeObjects;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class Espresso extends Beverage{
    public Espresso(){
        this.description = "Espresso";//浓缩咖啡
    }

    @Override
    public double cost() {
        return 1.99;
    }
}
HouseBlends
package Chapter3_DecorativeObjects;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class HouseBlends extends Beverage{
    public HouseBlends(){
        this.description = "House Blend Coffee";//家常综合咖啡
    }

    @Override
    public double cost() {
        return 0.89;
    }
}
StarbuzzCoffee
package Chapter3_DecorativeObjects;

import Chapter3_DecorativeObjects.Decorators.Mocha;
import Chapter3_DecorativeObjects.Decorators.Soy;
import Chapter3_DecorativeObjects.Decorators.Whip;

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class StarbuzzCoffee {
    public static void main(String[] args) {
        Beverage beverage = new Espresso();//浓缩咖啡
        System.out.println(beverage.getDescription()+" $"+beverage.cost());

        Beverage beverage1 = new HouseBlends();
        beverage1 = new Mocha(beverage1);//在家常综合咖啡里面加入摩卡
        beverage1 = new Whip(beverage1);//再加入奶泡
        beverage1 = new Soy(beverage1);//再加入豆奶
        beverage1 = new Mocha(beverage1);//双倍摩卡
        System.out.println(beverage1.getDescription()+" $"+beverage1.cost());

        Beverage beverage2 = new HouseBlends();
        beverage2.setSize(Beverage.Size.VENTI);//选择大杯
        beverage2 = new Mocha(beverage2);//在家常综合咖啡里面加入摩卡
        beverage2 = new Whip(beverage2);//再加入奶泡
        beverage2 = new Soy(beverage2);//再加入豆奶
        beverage2 = new Mocha(beverage2);//双倍摩卡
        System.out.println(beverage2.getDescription()+" $"+beverage2.cost());
    }
}

 运行结果:

《HeadFirst设计模式(第二版)》第三章代码——装饰者模式_第2张图片

 

myJavaIO案例

LowerCaseInputStream
package Chapter3_DecorativeObjects.MyJavaIO;

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

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class LowerCaseInputStream extends FilterInputStream {

    public LowerCaseInputStream(InputStream in){
        super(in);//在任意一种输入组件的基础上加入我们自己的装饰
    }

    @Override
    public int read() throws IOException {
        //按字节为输入
        int c = in.read();
        //将每个字符转成小写
        //这里可以根据自己的需求更改
        return (c == -1 ? c : Character.toLowerCase((char)c));
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        //按字节数组为输入
        int result = read(b, off, len);
        for(int i = off; i< off+result;i++){
            b[i] = (byte) Character.toLowerCase((char)b[i]);
        }
        return result;
    }
}
InputTest
package Chapter3_DecorativeObjects.MyJavaIO;

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

/**
 * @Author 竹心
 * @Date 2023/8/3
 **/

public class InputTest {
    public static void main(String[] args) {
        int c;
        try{
            InputStream in = new LowerCaseInputStream(
                    new BufferedInputStream(
                            new FileInputStream("D:\\JAVA\\Code\\DesignPatterns\\src\\Chapter3_DecorativeObjects\\MyJavaIO\\test.txt")));
            while((c = in.read()) >= 0){
                System.out.print((char)c);
            }
            in.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

test.txt

Try My Best To Beat You!

运行结果:

《HeadFirst设计模式(第二版)》第三章代码——装饰者模式_第3张图片

 

你可能感兴趣的:(设计模式,java)