生成器模式

生成器模式的物件。適用性.在以下情況使用生成器模式：為建立一個Product物件的各個部件指定抽象介面。實現Builder的介面以構造和裝配該產品的各個部件。定義並明確它所建立的表示。提供一個檢索產品的介面構造一個使用Builder介面的物件。表示被構造的複雜物件。ConcreateBuilder建立該產品的內部表示並定義它的裝配過程。包含定義組成部件的類，包括將這些部件裝配成最終產品的介面。協作.客戶建立Director物件，並用它所想要的Builder物件進行配置。範例.Java./** "Product" */class Pizza {private String dough = "";private String sauce = "";private String topping = "";"/** "Abstract Builder" */"abstract class PizzaBuilder {protected Pizza pizza;public abstract void buildDough();public abstract void buildSauce();public abstract void buildTopping();/** "ConcreteBuilder" */class HawaiianPizzaBuilder extends PizzaBuilder {/** "ConcreteBuilder" */class SpicyPizzaBuilder extends PizzaBuilder {"/** "Director" */"class Waiter {private PizzaBuilder pizzaBuilder;public void constructPizza() {pizzaBuilder.createNewPizzaProduct();pizzaBuilder.buildDough();pizzaBuilder.buildSauce();pizzaBuilder.buildTopping();/** A customer ordering a pizza. */class BuilderExample {public static void main(String[] args) {Waiter waiter = new Waiter();PizzaBuilder hawaiianPizzabuilder = new HawaiianPizzaBuilder();PizzaBuilder spicyPizzabuilder = new SpicyPizzaBuilder();waiter.setPizzaBuilder ( hawaiianPizzabuilder );waiter.constructPizza();Pizza pizza = waiter.getPizza();PHP.//設計模式：生成器模式//Coder: rollenc ( http://www.rollenc.com )interface Builder {function buildPartA(); //建立部件A比如建立汽車車輪//建立部件B 比如建立汽車方向盤function buildPartB();//建立部件C 比如建立汽車發動機function buildPartC();//返回最後組裝成品結果 (返回最後裝配好的汽車)//成品的組裝過程不在這裡進行,而是轉移到下面的Director類中進行.//從而實現瞭解耦過程和部件//return Productfunction getResult();class Director {private $builder;public function __construct($builder ) {$this-&gt;builder = $builder;// 將部件partA partB partC最後組成複雜物件//這裡是將車輪 方向盤和發動機組裝成汽車的過程public function construct() {$this-&gt;builder-&gt;buildPartA();$this-&gt;builder-&gt;buildPartB();$this-&gt;builder-&gt;buildPartC();class ConcreteBuilder implements Builder {public $partA, $partB, $partC;public function buildPartA() {echo 'partA is builded' . "\n";public function buildPartB() {echo 'partB is builded' . "\n";public function buildPartC() {echo 'partC is builded' . "\n";public function getResult () {echo 'Return product.' . "\n";return 1;$builder = new ConcreteBuilder();$director = new Director( $builder );$director-&gt;construct();$product = $builder-&gt;getResult();C++.using namespace std;class Product{public:string ProductName;class ProductA: public Product{public:class ProductB:public Product{public:class Creator{public:inline Product *CreateProduct(void){return DoCreateProduct();protected:virtual Product *DoCreateProduct(void)=0;class CreatorA: public Creator{public:inline Product *DoCreateProduct(void)override{return new ProductA;};class CreatorB: public Creator{public:inline Product *DoCreateProduct(void)override{return new ProductB;};int main(int argc, char **argv){Creator *creator=new CreatorA;Product *product=creator-&gt;CreateProduct();cout«product-&gt;ProductName«endl;//"ProductA"delete creator;delete product;creator=new CreatorB;product=creator-&gt;CreateProduct();cout«product-&gt;ProductName«endl;//"ProductB"delete creator;delete product;return 0;生成器逐步的構造它們的產品。因此生成器介面必須足夠普遍，以便為各種型別的具體生成器構造產品。通常情況下，由具體生成器生成的產品，它們的表示相差是如此之大以至於給不同的產品以公共父類沒有太大意思。定義為空方法可以使客戶只重定義他們所感興趣的操作。相關模式.抽象工廠模式與生成器相似，因為它也可以建立複雜物件。主要的區別是生成器模式著重於一步步構造一個複雜物件。而抽象工廠模式著重於多個系列的產品物件。生成器在最後的一步返回產品，而對於抽象工廠來說，產品是立即返回的。組合模式通常是用生成器生成的。參考.設計模式：可複用面向物件軟體的基礎軟體工程筆記庫