package com.lonton.designpatterns;
class Drink
{
private String size;
private String type;
public void setSize(String size)
{
this.size = size;
}
public void setType(String type)
{
this.type = type;
}
public String getSize()
{
return size;
}
public String getType()
{
return type;
}
}
abstract public class DrinkBuilder
{
protected Drink drink;
public Drink getDrink()
{
return drink;
}
public void createDrink()
{
drink = new Drink();
}
abstract public void buildSize();
abstract public void buildType();
public static void main(String[] args)
{
Mechine mechine = new Mechine();
TeaBuilder teaBuilder = new TeaBuilder();
mechine.setDrinkBuilder(teaBuilder);
mechine.produceDrink();
Drink drink = mechine.getDrink();
System.out.println(drink.getSize());
System.out.println(drink.getType());
}
}
class TeaBuilder extends DrinkBuilder
{
@Override
public void buildSize()
{
// TODO Auto-generated method stub
drink.setSize("medium");
}
@Override
public void buildType()
{
// TODO Auto-generated method stub
drink.setType("tea");
}
}
class CoffeeBuilder extends DrinkBuilder
{
@Override
public void buildSize()
{
// TODO Auto-generated method stub
drink.setSize("large");
}
@Override
public void buildType()
{
// TODO Auto-generated method stub
drink.setType("coffee");
}
}
class Mechine
{
DrinkBuilder drinkBuilder;
public void setDrinkBuilder(DrinkBuilder builder)
{
drinkBuilder = builder;
}
public Drink getDrink()
{
return drinkBuilder.getDrink();
}
public void produceDrink()
{
drinkBuilder.createDrink();
drinkBuilder.buildSize();
drinkBuilder.buildType();
}
}
Builder 创建者模式
最新推荐文章于 2022-06-19 19:59:32 发布