建造者模式-一步步组装复杂的对象

2019-10-31  本文已影响0人  TurboSnail

介绍

定义

建造者模式是一种创建型模式,它的定义如下

建造者模式(Builder Pattern) - 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。又可以称为生成器模式

适用场景

建造者模式角色及类图

建造者模式角色以及职责

UML类图

工厂模式和建造者模式的区别

构建者模式和工厂模式很类似,区别在于构建者模式是一种个性化产品的创建,通过组装零配件而创建一个新产品。而工厂模式是一种标准化的产品创建,不关心产品的构建过程,只关心什么产品是由什么工厂产生的

实例

简单实例

以一个点餐的过程为例,我们去到快餐店,每个人都可以根据自己的喜好自由搭配食物和饮料,最后生成的订单也不尽相同,如下

产品类

public class Meal {
    private List<String> foods;
    private List<String> drinks;

    public Meal() {
        this.foods = new ArrayList<>();
        this.drinks = new ArrayList<>();
    }

    public List<String> getFoods() {
        return foods;
    }

    public void setFoods(List<String> foods) {
        this.foods = foods;
    }

    public List<String> getDrinks() {
        return drinks;
    }

    public void setDrinks(List<String> drinks) {
        this.drinks = drinks;
    }

    @Override
    public String toString() {
        return "Meal{" + "foods=" + foods + ", drinks=" + drinks + '}';
    }
}

具体建造类

public class MealBuilder {
    private Meal meal;

    public MealBuilder() {
        meal = new Meal();
    }

    public MealBuilder addFood(String foodName) {
        meal.getFoods().add(foodName);
        return this;
    }

    public MealBuilder addDrink(String drinkName) {
        meal.getDrinks().add(drinkName);
        return this;
    }

    public Meal build() {
        return meal;
    }
}

指挥者/测试

public class MealDirector {
    @Test
    public void test(){
        MealBuilder mealBuilder = new MealBuilder();
        // 组装一个产品
        Meal meal = mealBuilder.addFood("汉堡").addFood("鸡肉卷").addDrink("可乐").build();
        System.out.println(meal);
    }
}

测试

Meal{foods=[汉堡, 鸡肉卷], drinks=[可乐]}

模板方法模式扩展

上面的例子,在构建实例的时候要我们按照需求动态的组装零配件,例如用餐的搭配组合。另外,我们也可以使用模板方法模式对建造者模式进行扩展,把零配件的组装屏蔽封装,例如我们在点餐的时候会有一些套餐,这些套餐就是模板,已经帮我们搭配好零配件

抽象建造者

public abstract class AbstractMealBuilder {
    protected Meal meal;

    public AbstractMealBuilder() {
        meal = new Meal();
    }

    public abstract void buildFood();

    public abstract void buildDrink();

    public Meal build() {
        return meal;
    }
}

具体建造者:A套餐

public class ConcreteMealBuilderA extends AbstractMealBuilder{
    @Override
    public void buildFood() {
        meal.getFoods().add("辣鸡堡");
        meal.getFoods().add("鸡肉卷");
    }

    @Override
    public void buildDrink() {
        meal.getDrinks().add("可乐");
    }
}

具体建造者:B套餐

public class ConcreteMealBuilderB extends AbstractMealBuilder{
    @Override
    public void buildFood() {
        meal.getFoods().add("牛肉堡");
        meal.getFoods().add("薯条");
    }

    @Override
    public void buildDrink() {
        meal.getDrinks().add("橙汁");
    }
}

指挥者

public class Director {
    private AbstractMealBuilder mealBuilder;

    public Director(AbstractMealBuilder mealBuilder) {
        this.mealBuilder = mealBuilder;
    }

    public Meal construct() {
        mealBuilder.buildFood();
        mealBuilder.buildDrink();
        return mealBuilder.build();
    }
}

测试

public class DirectorDemo {
    @Test
    public void test() {
        AbstractMealBuilder mealBuilderA = new ConcreteMealBuilderA();
        Director directorA = new Director(mealBuilderA);
        System.out.println("A套餐:" + directorA.construct());

        AbstractMealBuilder mealBuilderB = new ConcreteMealBuilderB();
        Director directorB = new Director(mealBuilderB);
        System.out.println("B套餐:" + directorB.construct());
    }
}

测试结果

A套餐:Meal{foods=[辣鸡堡, 鸡肉卷], drinks=[可乐]}
B套餐:Meal{foods=[牛肉堡, 薯条], drinks=[橙汁]}
上一篇下一篇

猜你喜欢

热点阅读