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

建造者模式的使用场景:

  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. 简单的Postman,还能玩出花?

    Postman是一款我们在工作中使用频率非常高的API调试工具,估计很多童鞋在使用它时也比较粗暴,填好接口地址.参数,直接send就完事了,估计大家要说了,这么简单的东西还能玩出什么花来.今天就和大家 ...

  2. openresty——yum安装 配置 使用 错误处理 docker方式安装

    yum方式安装 wget https://openresty.org/package/centos/openresty.repo mv openresty.repo /etc/yum.repos.d/ ...

  3. Java面向对象12——static详解

    static  package oop.demon01.demon07; ​ // static : public class Student { ​     private static int a ...

  4. 大厂需要什么样的 Android 开发?

    前言 昨天和一个百度的朋友闲聊,他说根据最近招聘 Android工程师的经验来看,大部分候选人在工作 3 年的时候基本都会遇上一道难过的坎. 为啥这么说呢? 因为工作一段时间之后,大部分工程师都已经完 ...

  5. Android程序员提加薪被拒,刷2000题跳槽涨薪50%!

    为什么想跳槽? 简单说一下当时的状况,我在这家公司做了两年多,这两年多完成了一个大项目,作为开发的核心主力,开发压力很大,特别是项目上线前的几个月是非常辛苦,几乎每晚都要加班到12点以后,周末最多只有 ...

  6. curl的基本使用

    基本使用 1. 初始化 初始化非常简单,只需要调用curl_init()函数即可,他会返回一个curl句柄,后边几乎其他关于curl的设置,关闭等函数都需要使用这个句柄 $curl = curl_in ...

  7. ElasticSearch版本控制--java实现

    一.前言 最近工作中有这样一个ElasticSearch(以下简称ES)写入的场景,Flink处理完数据实时写入ES.现在需要将一批历史数据通过Flink加载到到ES,有两个点需要保证: 对于历史数据 ...

  8. Distribute SSH Pubkey to Multiple Hosts with Fabric

    Generate ssh keys on source host with ssh-keygen; Disable known_hosts prompt(optional): add "St ...

  9. Java之Cookie与Session

    Cookie.Session Cookie:服务端生成Cookie发给客户端用于认证 Session:服务端进行进行登记,每人有不同的Session session与cookie的区别 Cookie: ...

  10. C#给线程传递数据

    目录 0. 前情说明 1. ParameterizedThreadStart类型的委托 2. 使用自定义类 3. 使用Lambda表达式 4. 参考以及文中源代码下载 shanzm-2021年8月24 ...