golang 设计模式-Creational

创建型模式:Singleton、Builder、Factory Method、Abstract Factory、PrototypeSingleton 单例设计模式having a unique instance of a type in the entire program在整个程序中只具有某一类型的唯一实例示例:唯一的计数器package creationaltype singleton struct { count int}var instance *singletonfunc
摘要由CSDN通过智能技术生成

创建型模式:Singleton、Builder、Factory Method、Abstract Factory、Prototype

Singleton 单例设计模式

having a unique instance of a type in the entire program
在整个程序中只具有某一类型的唯一实例

示例:唯一的计数器

package creational
type singleton struct {
   
	count int
}

var instance *singleton

func GetInstance() *singleton {
   
	if instance == nil {
   
		instance = new(singleton)
	}
	return new(singleton)
}
func (s *singleton) AddOne() int {
   
	s.count++
	return s.count
}

测例 go test -v ./patterns/creational -cover

package creational
import (
	"testing"
)

func Test_singleton_AddOne(t *testing.T) {
   
	c1 := GetInstance()
	currentCount := c1.AddOne()
	if currentCount != 1 {
   
		t.Errorf("After calling for the first time to count, the count must be1 but it is %d\n", currentCount)
	}
	c2 := GetInstance()
	currentCount = c2.AddOne()
	if currentCount != 2 {
   
		t.Errorf("After calling 'AddOne' using the second counter, the currentcount must be 2 but was %d\n", currentCount)
	}
}

func TestGetInstance(t *testing.T) {
   
	c1 := GetInstance()
	c2 := GetInstance()
	if c1 == nil || c2 == nil {
   
		t.Errorf("expected pointer to Singleton after calling GetInstance(), not nil")
	}
	if c1 != c2 {
   
		t.Errorf("Expected same instance in counter2 but it got a different instance")
	}

}

Builder 建造者设计模式

reusing an algorithm to create many implementations of an interface
重用某个算法来创建接口的多个实现

例如,你将使用几乎相同的技术来建造一辆汽车,你将建造一辆公共汽车,除了他们将不同的大小和座位的数量,所以我们为什么不重复建造过程(组装结构,放置车轮,放置座椅)?
示例:车辆制造
核心思想:ManufacturingDirector负责重用建造过程

package creational

//产品接口,开放更改
type VehicleProduct struct {
   
	Wheels    int
	Seats     int
	Structure string
}

//定义一组车辆建造的行为
type BuildProcess interface {
   
	SetWheels() BuildProcess
	SetSeats() BuildProcess
	SetStructure() BuildProcess
	Build() VehicleProduct
}

//----------导演
type ManufacturingDirector struct {
   
	bp BuildProcess
}

//使用BuildProcess进行产品构造
func (f *ManufacturingDirector) Construct() {
   
	//Implementation goes here
	f.bp.SetSeats().SetWheels().SetStructure()
}
func (f *ManufacturingDirector) SetBuilder(b BuildProcess) {
   
	//Implementation goes here
	f.bp = b
}

//----------小车建造器
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) Build() VehicleProduct {
    return c.v }

//--------摩托车建造器
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) Build() VehicleProduct {
    return b.v }

测例

package creational

import &#
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值