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

建造者模式的使用场景:

  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 -- 长字符串

    如果需要写一个非常非常长的字符串,它需要跨多行,那么,可以使用三个引号代替普通引号. print '''This is a very long string. It continues here. A ...

  2. header.mapper 用法

    const header= [xxxx,xxxx] 基本用法是 const obj = header.map( item=>{ obj= item+'123'; return  obj }) c ...

  3. mysql采坑笔记

    mysqld --initialize-insecure // 初始化数据 mysql -u root -p // 登录 navicat for mysql 1251错误解决方法 ALTER USER ...

  4. 在Linearlayout中新增ScrollView支持滚动

    https://blog.csdn.net/wenzhi20102321/article/details/53491176 1.一般只需要在布局中加个ScrollView即可 2.如果布局中包含lis ...

  5. 软件或jar包版本的小知识---Beta版、Final版、Free版等

    对于各种软件或jar包,其后面总有不同的"尾巴",如: 等,刚开始接触的肯定有些不知道.那么他们到底代表什么意思呢? 0.Release:发布版 1.Beta版:产品发布之前的测试 ...

  6. dubbo学习实践(2)之Dubbo入门Demo

    开篇之前,先来了解下dubbo服务治理与技术架构,直接看图 dubbo技术架构图: 1. 新建dubbo项目,目录结构如下 代码说明: 1.代码分为Provider(服务提供方)与consumer(服 ...

  7. Kong网关安装之Docker版(2)

    1.安装kong管理工具:konga或者kong-dashboard,这里选择konga 拉取konga镜像: sudo docker pull pantsel/konga:0.14.4 初始化kon ...

  8. Java面试常见基础问题

    1.equals和==有什么区别? ==比较两个对象在内存里是不是同一个对象,就是说在内存里的存储位置一致. 如:两个String对象存储的值是一样的,但是可能在内存里存储在不同的地方. equals ...

  9. 题解 marshland

    传送门 是个最大费用可行流 这题的建边很毒瘤 首先有危险度的点肯定要拆点 关键在于其它点怎么办 如果拆了不好保证每个点只经过一次 不拆连网络流都跑不了 但仔细观察题面,不能不难(???)发现一个L中那 ...

  10. asp.net core 搭建WebAPI微服务-----cosnul服务

    参考网址:https://blog.csdn.net/weixin_42084199/article/details/108643555 在此之前需要准备的是: vs2019,以往版本不支持dotne ...