設計模式 - 生成器模式(Builder) -开发者知识库

設計模式 - 生成器模式(Builder) -开发者知识库,第1张

Builder模式 將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創建不同的表示。
類圖:
設計模式 - 生成器模式(Builder) -开发者知识库,這里寫圖片描述,第2张
B u i l d e r對象提供給導向器(Director)一個構造產品的抽象接口;
每個具體生成器C o n c r e t e B u i l d e r包含了創建和裝配一個特定產品的所有代碼;改變該產品的內部表示時所要做的只是定義一個新的生成器。

案例1:不同的生成器構建不同的產品。
每份的套餐構成算法相同: 一個米飯 一份葷菜 一份素菜 一份湯
套餐A: 一份米飯 一份排骨 一份生菜 一份雞湯
套餐B : 一份米飯 一份牛肉 一份白菜 一份排骨湯
不同的套餐有不同的表示。

public abstract class Food {
    protected String name;
    public String getName() {
        return name;
    }
}
public class Rice extends Food {
    public Rice(String name) {
        this.name = name;
    }
}

public class Meat extends Food {
    public Meat(String name) {
        this.name = name;
    }
}
public class Vegetable extends Food {
    public Vegetable(String name) {
        this.name = name;
    }
}
public class Soap extends Food {
    public Soap(String name) {
        this.name = name;
    }
}
public class Meal {
    List<Food> meal = new ArrayList<Food>();
    public Meal() {}
    public void add(Food food) {
        meal.add(food);
    }
    public void remove(Food food) {
        meal.remove(food);
    }   
}
public interface MealBuilder{
    public void addRice();
    public void addMeat();
    public void addVegetable();
    public void addSoap();
    public Meal buildMeal();
}
public class MealBuilderA implements MealBuilder {
    private Meal meal  = new Meal();
    public void addRice() {
        meal.add(new Rice("normal"));
    }
    public void addMeat() {
        meal.add(new Meat("chopstick"));
    }
    public void addVegetable() {
        meal.add(new Vegetable("lettuce"));
    }
    public void addSoap() {
         meal.add(new Soap("chicken"));
    }
    public Meal buildMeal() {
        return meal;
    }
}
public class MealBuilderB implements MealBuilder {
    private Meal meal  = new Meal();
    public void addRice() {
        meal.add(new Rice("normal"));
    }
    public void addMeat() {
        meal.add(new Meat("beef"));
    }
    public void addVegetable() {
        meal.add(new Vegetable("cabbage"));
    }
    public void addSoap() {
         meal.add(new Soap("chopstick"));
    }
    public Meal buildMeal() {
        return meal;
    }
}

當客戶點套餐A時,服務員(Director)根據MealBuilderA 構建A套餐。

public class MealDirector {
    public Meal createMeal(MealBuilder builder) {
        builder.addRice();
        builder.addMeat();
        builder.addVegetable();
        builder.addSoap();
        return builder.buildMeal();
    }
}

案例2:同一生成器構建不同的產品。以上面的套餐為例。

public interface XMealBuilder{
    public void addRice(String name);
    public void addMeat(String name);
    public void addVegetable(String name);
    public void addSoap(String name);
    public Meal buildMeal();
}
public class ConcreteXMealBuilder implements XMealBuilder {
    private Meal meal  = new Meal();
    public void addRice(String name) {
        meal.add(new Rice(name));
    }
    public void addMeat(String name) {
        meal.add(new Meat(name));
    }
    public void addVegetable(String name) {
        meal.add(new Vegetable(name));
    }
    public void addSoap(String name) {
         meal.add(new Soap(name));
    }
    public Meal buildMeal() {
        return meal;
    }
}
public class XMealDirector {
    public Meal createMealA(XMealBuilder builder) {
        builder.addRice("normal");
        builder.addMeat("chopstick");
        builder.addVegetable("lettuce");
        builder.addSoap("chicken");
        return builder.buildMeal();
    }
    public Meal createMealB(XMealBuilder builder) {
        builder.addRice("normal");
        builder.addMeat("beef");
        builder.addVegetable("cabbage");
        builder.addSoap("chopstick");
        return builder.buildMeal();
    }    
}

案例3:
當一個類的構造器的參數列表很長時,可以用公有靜態內部類作為輔助類構建對象。

public class Person {
    private String id;
    private String name;
    private int age;
    private String phoneNumber;
    private String address;
    private String postCode;
    private Person(Builder builder) {
        this.id   = builder.id;
        this.name = builder.name; 
        this.age  = builder.age;
        this.phoneNumber = builder.phoneNumber;
        this.address     = builder.address;
        this.postCode    = builder.postCode;
    }
    public static class Builder {
        private String id;
        private String name;
        private int age;
        private String phoneNumber;
        private String address;
        private String postCode;
        public Builder(String id, String name) {
            this.id = id;
            this.name = name;    
        }
        public Builder age(int age) {
            this.age = age;
            return this;
        } 
        public Builder phoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
            return this;
        }
        public Builder address(String address) {
            this.address = address;
            return this;
        }
        public Builder postCode(String postCode) {
            this.postCode = postCode;
            return this;
        }
        public Person build() {
            return new Person(this);
        }
     }
}                               

客戶代碼:

public class PersonClient {
    public static void main(String[] args) {
    Person.Builder builder = new Person.Builder("123456890", "Tom");
    Person person = builder.age(10).phoneNumber("13720000000").address("SZ").postCode("518000").build();        
    }
} 

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复