Builder Mode:
1. The Builder Pattern uses multiple simple objects to build a complex object step by step. This type of design pattern is a creational pattern, which provides an optimal way to create objects.
2. A Builder class will construct the final object step by step. The Builder class is independent of other objects.
- Intent: To separate a complex build from its representation so that the same build process can create different representations.
- When to use: When some basic components do not change, but their combinations change frequently.
- How to fix it: Separate change from change.
example:
Description: Let's assume a business case for a fast food restaurant, where a typical meal can be a burger and a cold drink. Burgers can be either Veg Burger or Chicken Burger, and they come in a carton. Cold drinks can be Coca-Cola (coke) or Pepsi (pepsi), they are bottled.
Step 1: Create an interface that represents food items and food packaging.
<?php
//表示食物条目的接口。
interface Item {
public function name();
public function packing();
public function price();
}
<?php
//表示食物包装的接口。
interface Packing {
public function pack();
}
Step 2: Create an abstract class that implements the Item interface, which provides default functionality.
<?php
//实现食物条目接口的汉堡抽象类
abstract class Burger implements Item {
//打包方法
public function packing() {
return new Wrapper();
}
public abstract function price();
}
<?php
//实现食物条目接口的冷饮抽象类
abstract class ColdDrink implements Item {
//打包方法
public function packing() {
return new Bottle();
}
public abstract function price();
}
Step 3: Create an entity class that implements the Packing interface.
<?php
//实现包装接口,纸盒包装的实例类
class Wrapper implements Packing {
public function pack() {
return "Wrapper";
}
}
<?php
//实现包装接口,瓶子包装的实例类
class Bottle implements Packing {
public function pack() {
return "Bottle";
}
}
Step 4: Create entity classes that extend Burger and ColdDrink.
<?php
//实现汉堡抽象类的蔬菜汉堡实体类
class VegBurger extends Burger {
public function price() {
return 25.0;
}
public function name() {
return "Veg Burger";
}
}
<?php
//实现汉堡抽象类的鸡肉汉堡实体类
class ChickenBurger extends Burger {
public function price() {
return 50.5;
}
public function name() {
return "Chicken Burger";
}
}
<?php
//实现冷饮抽象类的可口可乐实体类
class Coke extends ColdDrink {
public function price() {
return 30.0;
}
public function name() {
return "Coke";
}
}
<?php
//实现冷饮抽象类的百事可乐实体类
class Pepsi extends ColdDrink {
public function price() {
return 35.0;
}
public function name() {
return "Pepsi";
}
}
Step 5: Create a Meal class with the Item object defined above.
<?php
//进餐实体类
class Meal {
//存储食物对象的数组
private $items = array();
//添加食物对象方法
public function addItem($item){
$this->items[] = $item;
}
//获取食物的总价
public function getCost(){
$cost = 0;
foreach($this->items as $item) {
$cost += $item->price();
}
return $cost;
}
//显示食物对象属性
public function showItems(){
foreach($this->items as $item) {
print("Item : ".$item->name());
print(", Packing : ".$item->packing()->pack());
print(", Price : ".$item->price());
}
}
}
Step 6: Create a MealBuilder class, the actual builder class is responsible for creating the Meal object.
<?php
//食物对象创建类
class MealBuilder {
//准备蔬菜汉堡套餐方法
public function prepareVegMeal (){
$meal = new Meal();
$meal->addItem(new VegBurger());
$meal->addItem(new Coke());
return $meal;
}
//准备鸡肉汉堡套餐方法
public function prepareNonVegMeal (){
$meal = new Meal();
$meal->addItem(new ChickenBurger());
$meal->addItem(new Pepsi());
return $meal;
}
}
Step 7: BuiderPatternDemo uses MealBuider to demonstrate the Builder Pattern.
<?php
require 'Item.php';
require 'Packing.php';
require 'Burger.php';
require 'VegBurger.php';
require 'ChickenBurger.php';
require 'ColdDrink.php';
require 'Coke.php';
require 'Pepsi.php';
require 'Meal.php';
require 'MealBuilder.php';
require 'Wrapper.php';
require 'Bottle.php';
//建造者模式演示
class BuilderPatternDemo {
//演示方法
public static function main() {
//实例化食物对象创建类
$mealBuilder = new MealBuilder();
$vegMeal = $mealBuilder->prepareVegMeal();
print("Veg Meal");
//var_dump($vegMeal);die;
$vegMeal->showItems();
print("Total Cost: ".$vegMeal->getCost());
$nonVegMeal = $mealBuilder->prepareNonVegMeal();
print("\n\nNon-Veg Meal");
$nonVegMeal->showItems();
print("Total Cost: ".$nonVegMeal->getCost());
}
}
BuilderPatternDemo::main();