Golang生成器模式

栏目: Go · 发布时间: 5年前

内容简介:builder.gobuilder_test.go程序输出如下,

builder.go

// builder
package builder

type VehicleProduct struct {
    Wheels    int
    Seats     int
    Structure string
}

type BuildProcess interface {
    SetWheels() BuildProcess
    SetSeats() BuildProcess
    SetStructure() BuildProcess
    GetVehicle() VehicleProduct
}

//MenufacturingDirector Begin
type ManufacturingDiretcor struct {
    builder BuildProcess
}

func (f *ManufacturingDiretcor) Construct() {
    f.builder.SetWheels().SetSeats().SetStructure()
}

func (f *ManufacturingDiretcor) SetBuilder(b BuildProcess) {
    f.builder = b
}

//MenufacturingDirector End

//Car Builder Start implements BuildProcess
type CarBuilder struct {
    v VehicleProduct
}

func (c *CarBuilder) SetWheels() BuildProcess {
    c.v.Wheels = 4
    return c
}

func (c *CarBuilder) SetSeats() BuildProcess {
    c.v.Seats = 5
    return c
}

func (c *CarBuilder) SetStructure() BuildProcess {
    c.v.Structure = "Car"
    return c
}

func (c *CarBuilder) GetVehicle() VehicleProduct {
    return c.v
}

//Car Builder End

//Bike Builder Start implements BuildProcess
type BikeBuilder struct {
    v VehicleProduct
}

func (b *BikeBuilder) SetWheels() BuildProcess {
    b.v.Wheels = 2
    return b
}

func (b *BikeBuilder) SetSeats() BuildProcess {
    b.v.Seats = 2
    return b
}

func (b *BikeBuilder) SetStructure() BuildProcess {
    b.v.Structure = "Motorbike"
    return b
}

func (b *BikeBuilder) GetVehicle() VehicleProduct {
    return b.v
}

//Bike Builder End

builder_test.go

package builder

import (
    "testing"
)

func TestBuilderPattern(t *testing.T) {
    manufactureComplex := ManufacturingDiretcor{}

    carBuilder := &CarBuilder{}
    manufactureComplex.SetBuilder(carBuilder)
    manufactureComplex.Construct()
    car := carBuilder.GetVehicle()

    if car.Wheels != 4 {
        t.Errorf("Wheels on a car must be 4, but it is %d \n", car.Wheels)
    }

    if car.Structure != "Car" {
        t.Errorf("Structure Of a car must be 'Car', but it is %v \n", car.Structure)
    }

    if car.Seats != 5 {
        t.Errorf("Seats on a car must be 5, but it is %d \n", car.Seats)
    }

    bikeBuilder := &BikeBuilder{}
    manufactureComplex.SetBuilder(bikeBuilder)
    manufactureComplex.Construct()
    motoBike := bikeBuilder.GetVehicle()

    motoBike.Seats = 1
    if motoBike.Wheels != 2 {
        t.Errorf("Wheels on a motobike must be 2 but found %d \n", motoBike.Wheels)
    }

    if motoBike.Structure != "Motorbike" {
        t.Errorf("Structure of a motobike must be 'Motorbike' but found %v \n", motoBike.Structure)
    }
}

程序输出如下,

Golang生成器模式

image.png


以上所述就是小编给大家介绍的《Golang生成器模式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

重构

重构

Martin Fowler / 熊节 / 人民邮电出版社 / 2010 / 69.00元

重构,一言以蔽之,就是在不改变外部行为的前提下,有条不紊地改善代码。多年前,正是本书原版的出版,使重构终于从编程高手们的小圈子走出,成为众多普通程序员日常开发工作中不可或缺的一部分。本书也因此成为与《设计模式》齐名的经典著作,被译为中、德、俄、日等众多语言,在世界范围内畅销不衰。 本书凝聚了软件开发社区专家多年摸索而获得的宝贵经验,拥有不因时光流逝而磨灭的价值。今天,无论是重构本身,业界对重......一起来看看 《重构》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换