生成器模式
本文最后更新于 538 天前,其中的信息可能已经有所发展或是发生改变。

Builder Pattern 对象创建型模式

意图

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

结构

其中:

  • Builder 为创建一个 Product 对象的各个部件指定抽象接口
  • ConcreteBuilder 实现 Builder 的接口以构造和装配该产品的各个部件,定义并明确它所创建的表示,提供一个检索产品的接口
  • Director 构造一个使用 Builder 接口的对象
  • Product 表示被构建的复杂对象。ConcreteBuilder 创建该产品的内部表示并定义它的装配过程。包含定义组成组件的类,包括将这些组件装配成最终产品的接口

适用性

Builder 模式适用于:

  • 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式
  • 当构建过程必须允许被构造的对象有不同的表示时

例子 1

某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。前台服务员(Waiter)调度厨师制作套餐。现采用生成器模式实现制作过程。类图如下

生成器模式-例子

Java

class Pizza {
    private String parts;
    public void setParts(String parts) { this.parts = parts; }
    public String toString() { return this.parts; }
}

abstract class PizzaBuilder {
    protected Pizza pizza;
    public Pizza getPizza() { return pizza; }
    public void createNewPizza() { pizza = new Pizza(); }
    public abstract void buildParts();
}

class HawaiianPizzaBuilder extends PizzaBuilder {
    public void buildParts(){
        pizza.setParts("cross + mild + ham&pineapp1e");
    }
}

class SpicyPizzaBuilder extends PizzaBuilder {
    public void buildParts() {
        pizza.setParts("panbaked + hot + pepperoni&salami");
    }
}

class Waiter {
    private PizzaBuilder pizzaBuilder;
    public void setPizzaBuilder(PizzaBuilder pizzaBuilder) {/*设置构建器*/
        this.pizzaBuilder = pizzaBuilder;
    }
    public Pizza getPizza() { return pizzaBuilder.getPizza(); }
    public void construct() { /* 构建 */
        pizzaBuilder.createNewPizza();
        pizzaBuilder.buildParts();
    }
}
class FastFoodOrdering {
    public static void main(String[] args) {
        Waiter waiter = new Waiter();
        PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder();
        waiter.setPizzaBuilder(hawaiian_pizzabuilder);
        waiter.construct();
        System.out.println("pizza:" + waiter.getPizza());
        // 输出:pizza:cross + mild + ham&pineapp1e
    }
}

C++

#include <iostream>
#include <string>
using namespace std;

class Pizza
{
private:
    string parts;

public:
    void setParts(string parts) { this->parts = parts; }
    string getParts() { return parts; }
};

class PizzaBuilder
{
protected:
    Pizza *pizza;

public:
    Pizza *getPizza() { return pizza; }
    void createNewPizza() { pizza = new Pizza(); }
    virtual void buildParts() = 0;
};

class HawaiianPizzaBuilder : public PizzaBuilder
{
public:
    void buildParts() { pizza->setParts("cross + mild + ham&pineapple"); }
};

class SpicyPizzaBuilder : public PizzaBuilder
{
public:
    void buildParts()
    {
        pizza->setParts("pan baked + hot + pepperoni&salami");
    }
};

class Waiter
{
private:
    PizzaBuilder *pizzaBuilder;

public:
    void setPizzaBuilder(PizzaBuilder *pizzaBuilder)
    { /* 设置构建器 */
        this->pizzaBuilder = pizzaBuilder;
    }
    Pizza *getPizza() { return pizzaBuilder->getPizza(); }
    void construct()
    {
        pizzaBuilder->createNewPizza();
        pizzaBuilder->buildParts();
    }
};

int main()
{
    Waiter *waiter = new Waiter();
    PizzaBuilder *hawaiian_pizzabuilder = new HawaiianPizzaBuilder();
    waiter->setPizzaBuilder(hawaiian_pizzabuilder);
    waiter->construct();
    cout << "pizza: " << waiter->getPizza()->getParts() << endl;
}

// 程序的输出结果为:
// pizza:cross + mild + ham*pineapple

例子 2

import java.util.*;

public class Main{
    public static void main(String[] args) {
        Director d = new Director();
        Builder b1 = new Builder1();

        d.Construct(b1);
        Product p1 = new b1.getResult();
        p1.show();
    }
}

class Product{
    List<String> parts = new ArrayList<String>();

    public void Add(String part){
        parts.add(part);
    }

    public void show(){
        System.out.println("The information of product:");
        for(String s : parts)
            System.out.print(s + " ");
        System.out.println();
    }
}

abstract class Builder{
    public abstract void BuildPart();
    public abstract Product getResult();
}

class Builder1 extends Builder{
    Product p = new Product();

    @Override
    public void BuildPart(){
        p.Add("A1");
        p.Add("A2");
        p.Add("C");
    }

    @Override
    public Product getResult(){
        return p;
    }
}

class Builder2 extends Builder{
    Product p = new Product();

    @Override
    public void BuildPart(){
        p.Add("B1");
        p.Add("B2");
        p.Add("C");
    }

    @Override
    public Product getResult(){
        return p;
    }
}

class Director{
    public void Construct(Builder builder){
        builder.BuildPart();
    }
}
本文链接:生成器模式
本文章由 yexca 采用 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 进行许可,转载请注明出处。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇