建造者模式

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。

主要解决:主要解决在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。

应用实例: 1、去肯德基,汉堡、可乐、薯条、炸鸡翅等是不变的,而其组合是经常变化的,生成出所谓的"套餐"。


例子

MealBuilder类相当于一个点餐平台,Meal类则用来统计各种分类的总价格,Item就是其中某一道食物,可以是一个汉堡(Burger)和一杯冷饮(Cold drink)。汉堡又分为素食汉堡(Veg Burger)或鸡肉汉堡(Chicken Burger),放在纸盒中(Wrapper),冷饮又分为可口可乐(coke)或百事可乐(pepsi),放在瓶子中(Bottle)。

1、Item某样食物

1 public interface Item {
2     String name();
3     Packing packing();
4     float price();
5 }
Item

2、包装类

1 //包装类
2 public interface Packing {
3      String pack();
4 }
Packing

3、汉堡和冷饮(食物的两大类别)

1 public abstract  class ColdDrink implements  Item {
2     @Override
3     public Packing packing() {
4         return new Bottle();
5     }
6
7     @Override
8     public  abstract  float price();
9 }
ColdDrink
 1 //汉堡
 2 //普通实现类需要实现所有方法
 3 public abstract class Burger implements Item{
 4
 5     @Override
 6     public Packing packing() {
 7         return new Wrapper();
 8     }
 9     @Override
10     public abstract float price();
11 }
Burger

4、纸盒和瓶子(包装的两大类别)

1 public class Bottle implements  Packing{
2
3     @Override
4     public String pack() {
5         return "Bottle";
6     }
7 }
Bottle
1 //纸盒
2 public class Wrapper implements  Packing {
3
4     @Override
5     public String pack() {
6         return "Wrapper";
7     }
8 }
Wrapper

5、具体的每道菜

 1 public class ChickenBurger extends Burger {
 2     @Override
 3     public String name() {
 4         return "ChickenBurger";
 5     }
 6
 7     @Override
 8     public float price() {
 9         return 18.f;
10     }
11 }
ChickenBurger
 1 public class VgBurger extends Burger {
 2     @Override
 3     public String name() {
 4         return "VgBurger";
 5     }
 6
 7     @Override
 8     public float price() {
 9        return 25.0f;
10     }
11 }
VgBurger
 1 public class Coke extends  ColdDrink {
 2     @Override
 3     public String name() {
 4         return "Coke";
 5     }
 6
 7     @Override
 8     public float price() {
 9         return 4.0f;
10     }
11 }
Coke
 1 public class Pepsi extends ColdDrink {
 2     @Override
 3     public String name() {
 4         return "Pepsi";
 5     }
 6
 7     @Override
 8     public float price() {
 9         return 5.0f;
10     }
11 }
Pepsi

6、Meal类进行统计

 1 import java.util.ArrayList;
 2 import java.util.List;
 3
 4 public class Meal {
 5     private List<Item> items = new ArrayList<Item>();
 6     public void addItem(Item item){
 7         items.add(item);
 8     }
 9     public float getCost(){
10         float coast = 0.0f;
11         for(Item item:items){
12             coast +=item.price();
13         }
14         return coast;
15     }
16     public void shownItems(){
17         for(Item item:items){
18             System.out.println("Item : "+item.name());
19             System.out.println(", Packing : "+item.packing().pack());
20             System.out.println(", Price : "+item.price());
21         }
22     }
23
24 }
Meal

7、MealBuilder进行点餐

 1 public class MealBuilder {
 2     public Meal prepareVegMeal(){
 3         Meal meal = new Meal();
 4         meal.addItem(new VgBurger());
 5         meal.addItem(new Coke());
 6         return  meal;
 7     }
 8
 9     public  Meal prepareNoMeal(){
10         Meal meal = new Meal();
11         meal.addItem(new ChickenBurger());
12         meal.addItem(new Pepsi());
13         return  meal;
14     }
15 }
MealBuilder

8、测试类

 1 public class BuilderPatternDemo {
 2     public static void main(String[] args) {
 3         MealBuilder mealBuilder = new MealBuilder();
 4         Meal vegMeal = mealBuilder.prepareVegMeal();
 5         System.out.println("Veg Meal:");
 6         vegMeal.shownItems();
 7         System.out.println("Total Cost:"+vegMeal.getCost());
 8
 9
10         Meal nonMeal = mealBuilder.prepareNoMeal();
11         System.out.println("Non Meal");
12         nonMeal.shownItems();
13         System.out.println("Total Coast:"+ nonMeal.getCost());
14
15     }
16 }
BuilderPatternDemo

9、输出结果

 1 Veg Meal:
 2 Item : VgBurger
 3 , Packing : Bottle
 4 , Price : 25.0
 5 Item : Coke
 6 , Packing : Bottle
 7 , Price : 4.0
 8 Total Cost:29.0
 9 Non Meal
10 Item : ChickenBurger
11 , Packing : Bottle
12 , Price : 18.0
13 Item : Pepsi
14 , Packing : Bottle
15 , Price : 5.0
16 Total Coast:23.0
打印结果

参考转载自:https://www.runoob.com/design-pattern/builder-pattern.html

01-04 05:29