参考:https://www.yiibai.com/design_pattern/builder_pattern.html
使用简单的对象逐步构建复杂的对象
- 创建一个商品接口:有商品名称,打包方式,价格。
package Test0627;
public interface Item {
public String name();
public Packing packing();
public float price();
}
- 创建打包类,是个接口,提供打包方式方法
package Test0627;
public interface Packing {
public String pack();
}
- 实现打包接口
瓶装
package Test0627;
public class Bottle implements Packing {
@Override
public String pack() {
return "bottle...";
}
}
包装
package Test0627;
public class Wrapper implements Packing {
@Override
public String pack() {
return "wrapper...";
}
}
- 创建一个冰饮类(抽象类)实现商品接口,
重写打包方法,返回瓶装对象
将price()设为抽象方法
package Test0627;
public abstract class ColdDrink implements Item {
@Override
public Packing packing() {
return new Bottle();
}
@Override
public abstract float price();
}
- 创建一个汉堡类(抽象类)实现商品接口,
重写打包方法,返回包装对象,
将price()设为抽象方法
package Test0627;
public abstract class Burger implements Item{
@Override
public Packing packing(){
return new Wrapper();
}
@Override
public abstract float price();
}
- 创建蔬菜汉堡类,继承汉堡类,重写item里的name和price
package Test0627;
public class VegBurger extends Burger {
@Override
public String name() {
return "veg burger";
}
@Override
public float price() {
return 25.0f;
}
}
- 创建鸡肉汉堡类,继承汉堡类,重写item里的name和price
package Test0627;
public class ChickenBurger extends Burger {
@Override
public String name() {
return "chicken burger";
}
@Override
public float price() {
return 50.0f;
}
}
- 创建可乐类,继承冰饮,重写item里的name和price
package Test0627;
public class Coke extends ColdDrink {
@Override
public String name() {
return "coke";
}
@Override
public float price() {
return 12.0f;
}
}
- 创建百事类,继承冰饮,重写item里的name和price
package Test0627;
public class Pepsi extends ColdDrink {
@Override
public String name() {
return "pepsi";
}
@Override
public float price() {
return 15.0f;
}
}
9.完成了商品的信息,接下来要创建点餐类
package Test0627;
import java.util.ArrayList;
import java.util.List;
public class Meal {
//定义一个集合,存储item信息
private List<Item> iterms = new ArrayList<Item>();
//定义一个方法,往list集合里添加item信息
public void addItem(Item iterm){
iterms.add(iterm);
}
//定义一个方法,通过循环计算出list和里item的总价
public float getCost(){
float cost = 0.0f;
for(Item iterm : iterms){
cost += iterm.price();
}
return cost;
}
//定义一个方法,输出list每个item的信息
public void showItems(){
for(Item iterm:iterms){
System.out.print("Item: "+iterm.name());
System.out.print(", Packing: "+iterm.packing().pack());
System.out.println(", Price: "+ iterm.price());
}
}
}
- 创建组合餐类
package Test0627;
public class MealBuilder {
//蔬菜餐
public Meal prepareVegMeal(){
Meal meal = new Meal();
//添加蔬菜汉堡和可乐
meal.addItem(new VegBurger());
meal.addItem(new Coke());
return meal;
}
//鸡肉餐
public Meal prepareNonVegMeal(){
Meal meal = new Meal();
//添加鸡肉汉堡和百事
meal.addItem(new ChickenBurger());
meal.addItem(new Pepsi());
return meal;
}
}
- 测试一下
package Test0627;
public class BuilderPatternDemo {
public static void main(String[] args) {
MealBuilder mealBuilder = new MealBuilder();
Meal vegMeal = mealBuilder.prepareVegMeal();
System.out.println("Veg Meal");
vegMeal.showItems();
System.out.println("Total Cost:"+vegMeal.getCost());
Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
System.out.println("\n\nNon-Veg Meal");
nonVegMeal.showItems();
System.out.println("Total Cost: "+nonVegMeal.getCost());
}
}