Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 《Go语言设计模式》配套代码


#### ++++++++++++++++++++++++++++++++++++++++
#### 《Go语言设计模式》源码
#### ++++++++++++++++++++++++++++++++++++++++
#### Author:廖显东(ShirDon)
#### Blog: https://www.shirdon.com/
#### 作者微博:https://www.weibo.com/liaoxiandong
#### 作者知乎:https://www.zhihu.com/people/shirdonl
#### 仓库地址:https://gitee.com/shirdonl/goDesignPattern
#### 仓库地址:https://github.com/shirdonl/goDesignPattern
#### 交流咨询及反馈,请关注公众号"源码大数据"
#### ++++++++++++++++++++++++++++++++++++++++

# 图书展示
<img src="bookInfo/bookPic1.png" width = "400" height = "480" alt="图片名称" align=center />

#### 作者图书购买链接如下:
https://search.jd.com/Search?keyword=%E5%BB%96%E6%98%BE%E4%B8%9C&qrst=1&psort=3&psort=3&pvid=d7fa99f63997446eb8d86f0ebb13febe&click=2

# 代码安装步骤如下:
#### 安装步骤如下:
#### (1)下载源码;
#### (2)解压;
#### (3)将解压的文件夹gitee.com或者github.com复制到$GOPATH/src目录下,如果gitee.com或者github.com目录已经存在,请将子目录shirdonl下的文件夹复制到gitee.com或者github.com目录中。

![qrcode](bookInfo/codebigdata.jpg)
#### 假如读者在阅读本书的过程中有任何疑问,请关注“源码大数据”公众号,并按照提示输入读者的问题,作者会尽快进行交流回复。
#### 注意:开源仓库不包含第6章代码,要获取第6章源码,请购买本书通过前言的说明获取。







Binary file added bookInfo/bookPic1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bookInfo/bookPic2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bookInfo/codebigdata.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapter2/.DS_Store
Binary file not shown.
40 changes: 40 additions & 0 deletions chapter2/abstractFactory/actualCombat/computer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//定义电脑接口
type AbstractComputer interface {
SetColor(color string)
SetSize(size int)
GetColor() string
GetSize() int
}

type Computer struct {
color string
size int
}

func (s *Computer) SetColor(color string) {
s.color = color
}

func (s *Computer) GetColor() string {
return s.color
}

func (s *Computer) SetSize(size int) {
s.size = size
}

func (s *Computer) GetSize() int {
return s.size
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

import "fmt"

//电子产品工厂
type InterfaceElectronicFactory interface {
MakePhone() AbstractPhone
MakeComputer() AbstractComputer
}

//获取电子产品工厂对象
func GetElectronicFactory(brand string) (InterfaceElectronicFactory, error) {
if brand == "Xiaomi" {
return &XiaomiFactory{}, nil
}

if brand == "Lenovo" {
return &LenovoFactory{}, nil
}

return nil, fmt.Errorf("%s", "error brand type")
}
35 changes: 35 additions & 0 deletions chapter2/abstractFactory/actualCombat/lenovo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//联想品牌工厂
type LenovoFactory struct {
}

//生产手机
func (n *LenovoFactory) MakePhone() AbstractPhone {
return &LenovoPhone{
Phone: Phone{
color: "Black",
size: 5,
},
}
}

//生产电脑
func (n *LenovoFactory) MakeComputer() AbstractComputer {
return &LenovoComputer{
Computer: Computer{
color: "White",
size: 14,
},
}
}
16 changes: 16 additions & 0 deletions chapter2/abstractFactory/actualCombat/lenovoComputer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//联想电脑
type LenovoComputer struct {
Computer
}
16 changes: 16 additions & 0 deletions chapter2/abstractFactory/actualCombat/lenovoPhone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//联想手机
type LenovoPhone struct {
Phone
}
40 changes: 40 additions & 0 deletions chapter2/abstractFactory/actualCombat/phone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//定义手机接口
type AbstractPhone interface {
SetColor(color string)
SetSize(size int)
GetColor() string
GetSize() int
}

type Phone struct {
color string
size int
}

func (s *Phone) SetColor(color string) {
s.color = color
}

func (s *Phone) GetColor() string {
return s.color
}

func (s *Phone) SetSize(size int) {
s.size = size
}

func (s *Phone) GetSize() int {
return s.size
}
35 changes: 35 additions & 0 deletions chapter2/abstractFactory/actualCombat/xiaomi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//小米品牌工厂
type XiaomiFactory struct {
}

//生产手机
func (a *XiaomiFactory) MakePhone() AbstractPhone {
return &XiaomiPhone{
Phone: Phone{
color: "White",
size: 5,
},
}
}

//生产电脑
func (a *XiaomiFactory) MakeComputer() AbstractComputer {
return &XiaomiComputer{
Computer: Computer{
color: "Black",
size: 14,
},
}
}
16 changes: 16 additions & 0 deletions chapter2/abstractFactory/actualCombat/xiaomiComputer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//小米电脑
type XiaomiComputer struct {
Computer
}
16 changes: 16 additions & 0 deletions chapter2/abstractFactory/actualCombat/xiaomiPhone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package actualCombat

//小米手机
type XiaomiPhone struct {
Phone
}
48 changes: 48 additions & 0 deletions chapter2/abstractFactory/example/abstractFactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言设计模式》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 仓库地址:https://gitee.com/shirdonl/goDesignPattern
// 仓库地址:https://github.com/shirdonl/goDesignPattern
// 交流咨询,请关注公众号"源码大数据"
//++++++++++++++++++++++++++++++++++++++++

package example

import (
"fmt"
)

// 抽象产品接口
type AbstractProduct interface {
GetName()
}

//具体产品
type ConcreteProduct struct {
}

//具体产品的方法
func (c *ConcreteProduct) GetName() {
fmt.Println("具体产品 ConcreteProduct")
}

// 抽象工厂接口
type AbstractFactory interface {
CreateProduct() AbstractProduct
}

//具体工厂
type ConcreteFactory struct {
}

// 初始化具体工厂对象
func NewConcreteFactory() ConcreteFactory {
return ConcreteFactory{}
}

//具体工厂创建具体产品
func (s *ConcreteFactory) CreateProduct() ConcreteProduct {
return ConcreteProduct{}
}
Loading