// 工厂模式interface CoffeeFactory { Coffee createCoffee(String coffeeType); Condiment createCo

// 工厂模式
interface CoffeeFactory {
    Coffee createCoffee(String coffeeType);
    Condiment createCondiment(String condimentType);
}

// 具体工厂类
class ConcreteCoffeeFactory implements CoffeeFactory {
    @Override
    public Coffee createCoffee(String coffeeType) {
        if ("Espresso".equals(coffeeType)) {
            return new Espresso();
        } else if ("Americano".equals(coffeeType)) {
            return new Americano();
        } else if ("Latte".equals(coffeeType)) {
            return new Latte();
        }
        return null;
    }

    @Override
    public Condiment createCondiment(String condimentType) {
        if ("Milk".equals(condimentType)) {
            return new Milk();
        } else if ("Foam".equals(condimentType)) {
            return new Foam();
        } else if ("Syrup".equals(condimentType)) {
            return new Syrup();
        }
        return null;
    }
}

// 咖啡类
interface Coffee {
    double cost();
}

// 具体咖啡类
class Espresso implements Coffee {
    @Override
    public double cost() {
        return 2.0;
    }
}

class Americano implements Coffee {
    @Override
    public double cost() {
        return 2.5;
    }
}

class Latte implements Coffee {
    @Override
    public double cost() {
        return 3.0;
    }
}

// 装饰者模式
abstract class Condiment implements Coffee {
    protected Coffee coffee;

    public Condiment(Coffee coffee) {
        this.coffee = coffee;
    }
}

// 具体装饰者类
class Milk extends Condiment {
    public Milk(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double cost() {
        return coffee.cost() + 0.5;
    }
}

class Foam extends Condiment {
    public Foam(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double cost() {
        return coffee.cost() + 0.3;
    }
}

class Syrup extends Condiment {
    public Syrup(Coffee coffee) {
        super(coffee);
    }

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

// 命令模式
interface OrderCommand {
    void execute();
}

// 具体命令类
class MakeCoffeeCommand implements OrderCommand {
    private Coffee coffee;

    public MakeCoffeeCommand(Coffee coffee) {
        this.coffee = coffee;
    }

    @Override
    public void execute() {
        System.out.println("Making coffee: $" + coffee.cost());
    }
}

// 客户端代码
public class CoffeeShop {
    public static void main(String[] args) {
        CoffeeFactory factory = new ConcreteCoffeeFactory();

        Coffee espresso = factory.createCoffee("Espresso");
        Coffee americano = factory.createCoffee("Americano");
        Coffee latte = factory.createCoffee("Latte");

        Condiment milkLatte = factory.createCondiment("Milk")(latte);
        Condiment syrupMilkLatte = factory.createCondiment("Syrup")(milkLatte);

        OrderCommand[] commands = {
                new MakeCoffeeCommand(espresso),
                new MakeCoffeeCommand(americano),
                new MakeCoffeeCommand(syrupMilkLatte)
        };

        for (OrderCommand command : commands) {
            command.execute();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值