建造者模式是一种创建型模式,主要用来创建比较复杂的对象。

建造者模式的使用场景:

  1. 建造者模式通常适用于有多个构造器参数或者需要较多构建步骤的场景。使用建造者模式可以精简构造器参数的数量,让构建过程更有条理。
  2. 可以为同一个产品提供两个不同的实现。比如,在下面的代码中,为house类型创建了两个不同的实现:通过iglooBuilder构建的igloo(冰屋),以及通过cabinBuilder构建的cabin(木屋)
  3. 可以应用于构建过程不允许被中断的场景。仍然参考刚才的代码,house类型的对象要么彻底完成,要么压根没有创建,不会存在中间状态,这是因为struct director封装了相应的过程,中间状态仅存在于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)
}

End!

GoLang设计模式01 - 建造者模式的更多相关文章

  1. Java设计模式之建造者模式(Builder)

    前言: 最近一直在学习okHttp,也对其做了一些整理,okHttp和Retrofit结合大大加速我们的开发效率,源码里面采用了很多设计模式,今天我们来学习一下其中的设计模式之一建造者模式. 建造者模 ...

  2. C#设计模式(5)——建造者模式(Builder Pattern)

    一.引言 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成.例如一个采购系统中,如果需要采购员去采购一批电脑时,在这个实际需求中,电脑就是一个复杂的对象, ...

  3. 【GOF23设计模式】建造者模式

    来源:http://www.bjsxt.com/ 一.[GOF23设计模式]建造者模式详解类图关系 建造飞船 package com.test.Builder; public class AirShi ...

  4. C++设计模式之建造者模式(三)

    4.引入钩子方法的建造者模式 建造者模式除了逐步构建一个复杂产品对象外.还能够通过Director类来更加精细地控制产品的创建过程.比如添加一类称之为钩子方法(HookMethod)的特殊方法来控制是 ...

  5. 乐在其中设计模式(C#) - 建造者模式(Builder Pattern)

    原文:乐在其中设计模式(C#) - 建造者模式(Builder Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 建造者模式(Builder Pattern) 作者:webabc ...

  6. 折腾Java设计模式之建造者模式

    博文原址:折腾Java设计模式之建造者模式 建造者模式 Separate the construction of a complex object from its representation, a ...

  7. C#设计模式之四建造者模式(Builder Pattern)【创建型】

    一.引言 今天我们要讲讲Builder模式,也就是建造者模式,当然也有叫生成器模式的,英文名称是Builder Pattern.在现实生活中,我们经常会遇到一些构成比较复杂的物品,比如:电脑,它就是一 ...

  8. Golang设计模式—简单工厂模式(Simple Factory Pattern)

    Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...

  9. Java 设计模式之建造者模式(四)

    原文地址:Java 设计模式之建造者模式(四) 博客地址:http://www.extlight.com 一.前言 今天继续介绍 Java 设计模式中的创建型模式--建造者模式.上篇设计模式的主题为 ...

随机推荐

  1. (python函数03)zip()函数

    (python函数03)zip()函数 zip是用来压缩的,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个元组(tuple),然后返回有这些tuples组成的对象,可强制转化为列表和 ...

  2. Linux从头学05-系统启动过程中的几个神秘地址,你知道是什么意思吗?

    作 者:道哥,10+年的嵌入式开发老兵. 公众号:[IOT物联网小镇],专注于:C/C++.Linux操作系统.应用程序设计.物联网.单片机和嵌入式开发等领域. 公众号回复[书籍],获取 Linux. ...

  3. ZooKeeper(2181、2171) 未授权访问

    下载地址http://archive.apache.org/dist/zookeeper/zookeeper-3.4.14/ tar -xzvf zookeeper-3.4.14.tar.gz cd ...

  4. 白话JavaScript原型链和继承

    原型基础 每个函数都有一个prototype属性,指向函数的原型对象 每个对象都一个私有属性 __proto__, 默认指向其构造函数的prototype 在JS中所有函数都是Function构造出来 ...

  5. 给每个li延时添加样式动画效果(setInterval,clearInterval)

    btnsAnime($('ul li')) function btnsAnime(pagesl) { var that = this $(pagesl).hide() let i = 0; funct ...

  6. 【原创】在macOS Big Sur (Silicon M1, ARM)中配置ASP运行环境

    亲测有效,转载请附原文地址. 一,安装Parallels Desktop,注意选择支持ARM的版本. 二,注册 Windows Insider Preview Downloads 账号,通过以下链接下 ...

  7. Redis内存碎片

    内存碎片大家都已经耳熟能详了.当Redis数据删除后,Redis释放的内存空间可能不是连续的,这就会带来一个问题,这些不连续的内存空间有可能处于闲置的,但是redis缺无法来保存数据,这就会减低Red ...

  8. win7环境下配置JDK&&安装Weblogic12.2.1.4.0

    win7环境下安装Weblogic12.2.1.4.0 写在前面 最近因为想复现一下weblogic的CVE-2020-2555和CVE-2020-2883漏洞,需要weblogic环境,但是vulh ...

  9. Error running 'Tomcat 9.0.24': port out of range:-1

    修改tomcat安装目录下的conf--server.xml检查一下,端口不能是-1, 一般会选80,或者1-65535之间的任意一个整数

  10. 并发编程 Process 互斥锁

    进程理论 程序与进程的区别 ''' 程序不是存在硬盘上的代码,相对来说是静态的 进程表示程序在执行的过程,是动态的 ''' 进程的调度 先来先服务调度算法 '''对长作业有利,对短作业无益''' 短作 ...