美文网首页
Golang生成器模式

Golang生成器模式

作者: FredricZhu | 来源:发表于2019-06-19 14:37 被阅读0次

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)
    }
}

程序输出如下,


image.png

相关文章

  • Golang生成器模式

    builder.go builder_test.go 程序输出如下,

  • 生成器模式(Builder Pattern)

    生成器模式:用生成器模式封装一个产品的构造过程,并允许按步骤构造 生成器模式又称为建造者模式,是创建性模式中的一种...

  • 设计模式--生成器(建造者)

    生成器模式(Builder) 生成器模式最初的定义出现于《设计模式》(Addison-Wesley,1994) 生...

  • 生成器模式(Builder Pattern)

    1.生成器模式概念 在《Head First Design Patterns》文中是这么介绍生成器模式的,“封装一...

  • golang xorm mysql代码生成器

    原来的代码生成器都是用java写的,心血来潮,把其中的生产golang数据层的代码,用golang实现了一下。 主...

  • golang使用go mod 模式安装gin

    golang使用go mod 模式安装gin 查看golang版本 确保golang版本1.11以上go vers...

  • 生成器模式

    生成器模式是iOS设计模式中比较简单的一种模式,也经常拿来和抽象工厂作对比。首先我们说下什么事生成器模式。该模式由...

  • iOS设计模式-生成器模式

    何为生成器模式? 生成器模式:将一个复杂对象的构建与它的表现分离,使得同样的构建过程可以创建不同的表现。 生成器模...

  • Golang 常见设计模式之单例模式

    之前我们已经看过了 Golang 常见设计模式中的装饰和选项模式,今天要看的是 Golang 设计模式里最简单的单...

  • 创建型模式4-生成器模式

    创建型模式4-生成器模式 生成器模式(builder) 意图 将一个复杂对象的构建与它的的表示分离,使得同样的构建...

网友评论

      本文标题:Golang生成器模式

      本文链接:https://www.haomeiwen.com/subject/rjhkqctx.html