1. 程式人生 > 其它 >GoLang設計模式01 - 建造者模式

GoLang設計模式01 - 建造者模式

建造者模式是一種建立型模式,主要用來建立比較複雜的物件。

建造者模式的使用場景:

  1. 建造者模式通常適用於有多個構造器引數或者需要較多構建步驟的場景。使用建造者模式可以精簡構造器引數的數量,讓構建過程更有條理。
  2. 可以為同一個產品提供兩個不同的實現。比如,在下面的程式碼中,為house型別建立了兩個不同的實現:通過iglooBuilder構建的igloo(冰屋),以及通過cabinBuilder構建的cabin(木屋)
  3. 可以應用於構建過程不允許被中斷的場景。仍然參考剛才的程式碼,house型別的物件要麼徹底完成,要麼壓根沒有建立,不會存在中間狀態,這是因為structdirector封裝了相應的過程,中間狀態僅存在於ConcreteBuilder中。

下面是UML類圖:

程式碼大致如下。

house.go:

package main

type house struct {
	windowType string
	doorType   string
	floor      int
}

iBuilder.go:

package main

type iBuilder interface {
	setWindowType()
	setDoorType()
	setNumFloor()
	getHouse() house
}

func getBuilder(builderType string) iBuilder {
	if builderType == "cabin" {
		return &cabinBuilder{}
	}
	if builderType == "igloo" {
		return &iglooBuilder{}
	}
	return nil
}

iglooBuilder.go

package main

type iglooBuilder struct {
	house
}

func newIglooBuilder() *iglooBuilder {
	return &iglooBuilder{}
}

func (b *iglooBuilder) setWindowType() {
	b.windowType = "Ice Window"
}

func (b *iglooBuilder) setDoorType() {
	b.doorType = "Ice Door"
}

func (b *iglooBuilder) setNumFloor() {
	b.floor = 1
}

func (b *iglooBuilder) getHouse() house {
	return b.house
}

cabinBuilder.go

package main

type cabinBuilder struct {
	house
}

func newCabinBuilder() *cabinBuilder {
	return &cabinBuilder{}
}

func (b *cabinBuilder) setWindowType() {
	b.windowType = "Wooden Window"
}

func (b *cabinBuilder) setDoorType() {
	b.doorType = "Wooden Door"
}

func (b *cabinBuilder) setNumFloor() {
	b.floor = 2
}

func (b *cabinBuilder) getHouse() house {
	return b.house
}

director.go

package main

type director struct {
	builder iBuilder
}

func newDirector(b iBuilder) *director {
	return &director{
		builder: b,
	}
}

func (d *director) setBuilder(b iBuilder) {
	d.builder = b
}

func (d *director) buildHouse() house {
	d.builder.setDoorType()
	d.builder.setWindowType()
	d.builder.setNumFloor()
	return d.builder.getHouse()
}

最後是main方法:

package main

import (
	"fmt"
)

func main() {
	cabinBuilder := getBuilder("cabin")
	iglooBuilder := getBuilder("igloo")

	director := newDirector(cabinBuilder)
	cabinHouse := director.buildHouse()

	fmt.Printf("Cabin House Door Type: %s\n", cabinHouse.doorType)
	fmt.Printf("Cabin House Window Type: %s\n", cabinHouse.windowType)
	fmt.Printf("Cabin House Num Floor: %d\n", cabinHouse.floor)

	director.setBuilder(iglooBuilder)
	iglooHouse := director.buildHouse()

	fmt.Printf("\nIgloo House Door Type: %s\n", iglooHouse.doorType)
	fmt.Printf("Igloo House Window Type: %s\n", iglooHouse.windowType)
	fmt.Printf("Igloo House Num Floor: %d\n", iglooHouse.floor)
}

程式碼已上傳到GitHub:zhyea / go-patterns / builder-pattern

End!


僅是學習筆記,難免出錯,望不吝指點