一、模式简介
定义:将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示,它是将一个复杂的对象分解为多个简单的对象,然后一步一步构建而成。它将变与不变相分离,即产品的组成部分是不变的,但每一部分是可以灵活选择的。
场景:创建的对象较复杂,由多个部件构成,各部件面临着复杂的变化,但构件间的建造顺序是稳定的。创建复杂对象的算法独立于该对象的组成部分以及它们的装配方式,即产品的构建过程和最终的表示是独立的。
- 角色结构:
- 产品角色(Product):它是包含多个组成部件的复杂对象,由具体建造者来创建其各个滅部件。
- 抽象建造者(Builder):它是一个包含创建产品各个子部件的抽象方法的接口,通常还包含一个返回复杂产品的方法。
- 具体建造者(Concrete Builder):实现 Builder 接口,完成复杂产品的各个部件的具体创建方法。
- 指挥官(Director):它调用建造者对象中的部件构造与装配方法完成复杂对象的创建,在指挥者中不涉及具体产品的信息。
二、经典模式实现
定义食品属性接口、抽象食品属性类、具体食品属性类
public interface Food { -> 产品角色
String name(); -> 名称
Double price(); -> 价格
Pack pack(); -> 包装
}
public abstract class AbstractHamburg implements Food { -> 抽象建造者
@Override
public abstract String name();
@Override
public abstract Double price();
@Override
public Pack pack() {
return new Paper();
}
}
public class ChickenHamburg extends AbstractHamburg { -> 具体建造者
@Override
public String name() {
return "炸鸡汉堡";
}
@Override
public Double price() {
return 15.00;
}
}
public abstract class AbstractCoke implements Food { -> 抽象建造者
@Override
public abstract String name();
@Override
public abstract Double price();
@Override
public Pack pack() {
return new Bottle();
}
}
public class CocaCola extends AbstractCoke { -> 具体建造者
@Override
public String name() {
return "可口可乐";
}
@Override
public Double price() {
return 5.00;
}
}
public class PepsiCola extends AbstractCoke { -> 具体建造者
@Override
public String name() {
return "百事可乐";
}
@Override
public Double price() {
return 5.00;
}
}
定义食品包装接口、具体包装实现类
public interface Pack { -> 抽象建造者
String pack();
}
public class Bottle implements Pack { -> 具体建造者
@Override
public String pack() {
return "瓶装";
}
}
public class Paper implements Pack { -> 具体建造者
@Override
public String pack() {
return "纸装";
}
}
定义套餐选择方式
public class Meal {
private List<Food> list = new ArrayList<>();
public boolean addFood(Food food) {
return list.add(food);
}
public boolean removeFood(Food food) {
return list.remove(food);
}
public Double price() {
BigDecimal decimal = BigDecimal.ZERO;
for (int i = 0; i < list.size(); i++) {
decimal = decimal.add(BigDecimal.valueOf(list.get(i).price()));
}
return decimal.doubleValue();
}
public void display() {
for (int i = 0; i < list.size(); i++) {
Food food = list.get(i);
System.out.println(food.pack().pack() + " " + food.name() + " " + food.price());
}
}
}
public class MealBuilder { -> 指挥官
public Meal ChickenCoca() {
Meal meal = new Meal();
meal.addFood(new ChickenHamburg());
meal.addFood(new CocaCola());
return meal;
}
public Meal ChickenPepsi() {
Meal meal = new Meal();
meal.addFood(new ChickenHamburg());
meal.addFood(new PepsiCola());
return meal;
}
}
选择套餐
MealBuilder builder = new MealBuilder();
Meal mealChickenCoca = builder.ChickenCoca();
mealChickenCoca.display();
Meal mealChickenPepsi = builder.ChickenPepsi();
mealChickenPepsi.display();
三、变种模式实现
套餐构造方法设置私有,通过内部提供的构造器完成实例化
public class Meal {
private List<Food> list = new ArrayList<>();
public Double price() {
BigDecimal decimal = BigDecimal.ZERO;
for (int i = 0; i < list.size(); i++) {
decimal = decimal.add(BigDecimal.valueOf(list.get(i).price()));
}
return decimal.doubleValue();
}
public void display() {
for (int i = 0; i < list.size(); i++) {
Food food = list.get(i);
System.out.println(food.pack().pack() + " " + food.name() + " " + food.price());
}
}
private Meal(MealBuilder builder) {
if(builder.list != null){
this.list = builder.list;
}
}
public static class MealBuilder {
private List<Food> list = new ArrayList<>();
public MealBuilder CocaCola() {
list.add(new CocaCola());
return this;
}
public MealBuilder PepsiCola() {
list.add(new PepsiCola());
return this;
}
public MealBuilder ChickenHamburg() {
list.add(new ChickenHamburg());
return this;
}
public Meal build() {
return new Meal(this);
}
}
}
选择套餐
Meal.MealBuilder builder = new Meal.MealBuilder();
Meal meal = builder.CocaCola().PepsiCola().ChickenHamburg().build();
meal.display();
网友评论