设计模式 - 建造者模式 Builder Pattern

简介

场景

在创建复杂对象时,用户无须关心该对象所包含的属性以及它们的组装方式,只需要指定复杂对象的类型和内容就可以构建它们。

模式定义

建造者模式:将一个复杂对象的构建与表示分离,使得同样的构建过程可以创建不同的表示。

模式特点

建造者模式包含四个角色:

  • Builder:抽象建造者
  • ConcreteBuilder:具体建造者
  • Director:指挥者
  • Product:产品角色

抽象建造者类中定义了产品的创建方法和返回方法;

建造者模式的结构中还引入了一个指挥者类Director,该类的作用主要有两个:一方面它隔离了客户与生产过程;另一方面它负责控制产品的生成过程。指挥者针对抽象建造者编程,客户端只需要知道具体建造者的类型,即可通过指挥者类调用建造者的相关方法,返回一个完整的产品对象

在客户端代码中,无须关心产品对象的具体组装过程,只需确定具体建造者的类型即可,建造者模式将复杂对象的构建与对象的表现分离开来,这样使得同样的构建过程可以创建出不同的表现。

优点

  • 产品本身与创建过程解耦,相同的创建过程通过对应具体建造者,可以创建不同的产品对象。
  • 符合“开闭原则”。

缺点

  • 如果产品之间的差异性很大,则不适合使用建造者模式
  • 如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大。

PHP 代码示例



abstract class AbstractPageBuilder {
    abstract function getPage();
}

abstract class AbstractPageDirector {
    abstract function __construct(AbstractPageBuilder $builder_in);
    abstract function buildPage();
    abstract function getPage();
}

class HTMLPage {
    private $page = NULL;
    private $page_title = NULL;
    private $page_heading = NULL;
    private $page_text = NULL;
    function __construct() {
    }
    function showPage() {
      return $this->page;
    }
    function setTitle($title_in) {
      $this->page_title = $title_in;
    }
    function setHeading($heading_in) {
      $this->page_heading = $heading_in;
    }
    function setText($text_in) {
      $this->page_text .= $text_in;
    }
    function formatPage() {
       $this->page  = '';
       $this->page .= ''</span><span class="token punctuation">.</span><span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">page_title</span><span class="token punctuation">.</span><span class="token single-quoted-string string">'';
       $this->page .= '';
       $this->page .= '

'.$this->page_heading.'

'
; $this->page .= $this->page_text; $this->page .= ''; $this->page .= ''; } } class HTMLPageBuilder extends AbstractPageBuilder { private $page = NULL; function __construct() { $this->page = new HTMLPage(); } function setTitle($title_in) { $this->page->setTitle($title_in); } function setHeading($heading_in) { $this->page->setHeading($heading_in); } function setText($text_in) { $this->page->setText($text_in); } function formatPage() { $this->page->formatPage(); } function getPage() { return $this->page; } } class HTMLPageDirector extends AbstractPageDirector { private $builder = NULL; public function __construct(AbstractPageBuilder $builder_in) { $this->builder = $builder_in; } public function buildPage() { $this->builder->setTitle('Testing the HTMLPage'); $this->builder->setHeading('Testing the HTMLPage'); $this->builder->setText('Testing, testing, testing!'); $this->builder->setText('Testing, testing, testing, or!'); $this->builder->setText('Testing, testing, testing, more!'); $this->builder->formatPage(); } public function getPage() { return $this->builder->getPage(); } } writeln('BEGIN TESTING BUILDER PATTERN'); writeln(''); $pageBuilder = new HTMLPageBuilder(); $pageDirector = new HTMLPageDirector($pageBuilder); $pageDirector->buildPage(); $page = $pageDirector->getPage(); writeln($page->showPage()); writeln(''); writeln('END TESTING BUILDER PATTERN'); function writeln($line_in) { echo $line_in."
"
; }

Golang 示例

package main

import "fmt"

// 抽象builder
type HouseBuilder struct {
    Floor string
    Wall int
    Window int
}
// 产品的生产步骤
type BuildProcesser interface {
    MakeFloor(string) BuildProcesser
    MakeWall(int) BuildProcesser
    MakeWindow(int) BuildProcesser
}

// 生产过程的指导者
type ManufactureDirector struct {
    builder BuildProcesser
}
func (d *ManufactureDirector) Construct() {
    d.builder.MakeFloor("red").MakeWall(4).MakeWindow(2)
}
func (d *ManufactureDirector) SetBuilder(b BuildProcesser) {
    d.builder = b
}

// 具体builder
type WoodHouseBuilder struct {
    h HouseBuilder
}
func (wh *WoodHouseBuilder) MakeFloor(s string) BuildProcesser {
    fmt.Println("WoodHouseBuilder MakeFloor: ", s)
    return wh
}
func (wh *WoodHouseBuilder) MakeWall(a int) BuildProcesser {
    fmt.Println("WoodHouseBuilder MakeWall: ", a)
    return wh
}
func (wh *WoodHouseBuilder) MakeWindow(a int) BuildProcesser {
    fmt.Println("WoodHouseBuilder MakeWindow: ", a)
    return wh
}

// 具体builder
type StoneHouseBuilder struct {
    h HouseBuilder
}
func (wh *StoneHouseBuilder) MakeFloor(s string) BuildProcesser {
    fmt.Println("StoneHouseBuilder MakeFloor: ", s)
    return wh
}
func (wh *StoneHouseBuilder) MakeWall(a int) BuildProcesser {
    fmt.Println("StoneHouseBuilder MakeWall: ", a)
    return wh
}
func (wh *StoneHouseBuilder) MakeWindow(a int) BuildProcesser {
    fmt.Println("StoneHouseBuilder MakeWindow: ", a)
    return wh
}

func main() {
    var md ManufactureDirector
    md = ManufactureDirector{}
    whb := &WoodHouseBuilder{}
    md.SetBuilder(whb)
    md.Construct()
    
    shb := &StoneHouseBuilder{}
    md.SetBuilder(shb)
    md.Construct()
}

你可能感兴趣的:(设计模式,builder,pattern,建造者模式)