Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
FangjieLiang committed Sep 13, 2022
1 parent 4c03513 commit ea1f5b3
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 66 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/golang-design-pattern.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.x
- 1.11.x
go:
- 1.x
- 1.11.x
script:
- go test -v ./...
- go test -v ./...
6 changes: 3 additions & 3 deletions 00_simple_factory/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 简单工厂模式

go 语言没有构造函数一说,所以一般会定义NewXXX函数来初始化相关类
NewXXX 函数返回接口时就是简单工厂模式,也就是说Golang的一般推荐做法就是简单工厂
`go` 语言没有构造函数一说,所以一般会定义 `NewXXX` 函数来初始化相关类
`NewXXX` 函数返回接口时就是简单工厂模式,也就是说 `Golang` 的一般推荐做法就是简单工厂

在这个simplefactory包中只有API 接口和NewAPI函数为包外可见,封装了实现细节。
在这个 `simplefactory` 包中只有API 接口和 `NewAPI` 函数为包外可见,封装了实现细节。
12 changes: 6 additions & 6 deletions 00_simple_factory/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package simplefactory

import "fmt"

//API is interface
// API is interface
type API interface {
Say(name string) string
}

//NewAPI return Api instance by type
// NewAPI return Api instance by type
func NewAPI(t int) API {
if t == 1 {
return &hiAPI{}
Expand All @@ -17,18 +17,18 @@ func NewAPI(t int) API {
return nil
}

//hiAPI is one of API implement
// hiAPI is one of API implement
type hiAPI struct{}

//Say hi to name
// Say hi to name
func (*hiAPI) Say(name string) string {
return fmt.Sprintf("Hi, %s", name)
}

//HelloAPI is another API implement
// helloAPI is another API implement
type helloAPI struct{}

//Say hello to name
// Say hello to name
func (*helloAPI) Say(name string) string {
return fmt.Sprintf("Hello, %s", name)
}
2 changes: 1 addition & 1 deletion 00_simple_factory/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package simplefactory

import "testing"

//TestType1 test get hiapi with factory
// TestType1 test get hiapi with factory
func TestType1(t *testing.T) {
api := NewAPI(1)
s := api.Say("Tom")
Expand Down
4 changes: 2 additions & 2 deletions 01_facade/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 外观模式

API 为facade 模块的外观接口,大部分代码使用此接口简化对facade类的访问
`API``facade` 模块的外观接口,大部分代码使用此接口简化对 `facade` 类的访问

facade模块同时暴露了a和b 两个Module 的NewXXX和interface,其它代码如果需要使用细节功能时可以直接调用。
`facade` 模块同时暴露了 `a``b` 两个 `Module``NewXXX``interface`,其它代码如果需要使用细节功能时可以直接调用。
12 changes: 6 additions & 6 deletions 01_facade/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ func NewAPI() API {
}
}

//API is facade interface of facade package
// API is facade interface of facade package
type API interface {
Test() string
}

//facade implement
// apiImpl facade implement
type apiImpl struct {
a AModuleAPI
b BModuleAPI
Expand All @@ -26,12 +26,12 @@ func (a *apiImpl) Test() string {
return fmt.Sprintf("%s\n%s", aRet, bRet)
}

//NewAModuleAPI return new AModuleAPI
// NewAModuleAPI return new AModuleAPI
func NewAModuleAPI() AModuleAPI {
return &aModuleImpl{}
}

//AModuleAPI ...
// AModuleAPI ...
type AModuleAPI interface {
TestA() string
}
Expand All @@ -42,12 +42,12 @@ func (*aModuleImpl) TestA() string {
return "A module running"
}

//NewBModuleAPI return new BModuleAPI
// NewBModuleAPI return new BModuleAPI
func NewBModuleAPI() BModuleAPI {
return &bModuleImpl{}
}

//BModuleAPI ...
// BModuleAPI ...
type BModuleAPI interface {
TestB() string
}
Expand Down
5 changes: 3 additions & 2 deletions 02_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

适配器模式用于转换一种接口适配另一种接口。

实际使用中Adaptee一般为接口,并且使用工厂函数生成实例。
实际使用中 `Adaptee` 一般为接口,并且使用工厂函数生成实例。

在Adapter中匿名组合Adaptee接口,所以Adapter类也拥有SpecificRequest实例方法,又因为Go语言中非入侵式接口特征,其实Adapter也适配Adaptee接口。
`Adapter` 中匿名组合 `Adaptee` 接口,所以 `Adapter` 类也拥有 `SpecificRequest` 实例方法,又因为 `Go` 语言中非入侵式接口特征,其实 `Adapter` 也适配 `Adaptee`
接口。
16 changes: 8 additions & 8 deletions 02_adapter/adapter.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package adapter

//Target 是适配的目标接口
// Target 是适配的目标接口
type Target interface {
Request() string
}

//Adaptee 是被适配的目标接口
// Adaptee 是被适配的目标接口
type Adaptee interface {
SpecificRequest() string
}

//NewAdaptee 是被适配接口的工厂函数
// NewAdaptee 是被适配接口的工厂函数
func NewAdaptee() Adaptee {
return &adapteeImpl{}
}

//AdapteeImpl 是被适配的目标类
// AdapteeImpl 是被适配的目标类
type adapteeImpl struct{}

//SpecificRequest 是目标类的一个方法
// SpecificRequest 是目标类的一个方法
func (*adapteeImpl) SpecificRequest() string {
return "adaptee method"
}

//NewAdapter 是Adapter的工厂函数
// NewAdapter 是Adapter的工厂函数
func NewAdapter(adaptee Adaptee) Target {
return &adapter{
Adaptee: adaptee,
}
}

//Adapter 是转换Adaptee为Target接口的适配器
// Adapter 是转换Adaptee为Target接口的适配器
type adapter struct {
Adaptee
}

//Request 实现Target接口
// Request 实现Target接口
func (a *adapter) Request() string {
return a.SpecificRequest()
}
2 changes: 1 addition & 1 deletion 03_singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
once sync.Once
)

//GetInstance 用于获取单例模式对象
// GetInstance 用于获取单例模式对象
func GetInstance() Singleton {
once.Do(func() {
instance = &singleton{}
Expand Down
4 changes: 2 additions & 2 deletions 03_singleton/singleton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func TestParallelSingleton(t *testing.T) {
instances := [parCount]Singleton{}
for i := 0; i < parCount; i++ {
go func(index int) {
//协程阻塞,等待channel被关闭才能继续运行
// 协程阻塞,等待channel被关闭才能继续运行
<-start
instances[index] = GetInstance()
wg.Done()
}(i)
}
//关闭channel,所有协程同时开始运行,实现并行(parallel)
// 关闭channel,所有协程同时开始运行,实现并行(parallel)
close(start)
wg.Wait()
for i := 1; i < parCount; i++ {
Expand Down
2 changes: 1 addition & 1 deletion 04_factory_method/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

工厂方法模式使用子类的方式延迟生成对象到子类中实现。

Go中不存在继承 所以使用匿名组合来实现
`Go` 中不存在继承 所以使用匿名组合来实现
22 changes: 11 additions & 11 deletions 04_factory_method/factorymethod.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
package factorymethod

//Operator 是被封装的实际类接口
// Operator 是被封装的实际类接口
type Operator interface {
SetA(int)
SetB(int)
Result() int
}

//OperatorFactory 是工厂接口
// OperatorFactory 是工厂接口
type OperatorFactory interface {
Create() Operator
}

//OperatorBase 是Operator 接口实现的基类,封装公用方法
// OperatorBase 是Operator 接口实现的基类,封装公用方法
type OperatorBase struct {
a, b int
}

//SetA 设置 A
// SetA 设置 A
func (o *OperatorBase) SetA(a int) {
o.a = a
}

//SetB 设置 B
// SetB 设置 B
func (o *OperatorBase) SetB(b int) {
o.b = b
}

//PlusOperatorFactory 是 PlusOperator 的工厂类
// PlusOperatorFactory 是 PlusOperator 的工厂类
type PlusOperatorFactory struct{}

func (PlusOperatorFactory) Create() Operator {
Expand All @@ -36,17 +36,17 @@ func (PlusOperatorFactory) Create() Operator {
}
}

//PlusOperator Operator 的实际加法实现
// PlusOperator Operator 的实际加法实现
type PlusOperator struct {
*OperatorBase
}

//Result 获取结果
// Result 获取结果
func (o PlusOperator) Result() int {
return o.a + o.b
}

//MinusOperatorFactory 是 MinusOperator 的工厂类
// MinusOperatorFactory 是 MinusOperator 的工厂类
type MinusOperatorFactory struct{}

func (MinusOperatorFactory) Create() Operator {
Expand All @@ -55,12 +55,12 @@ func (MinusOperatorFactory) Create() Operator {
}
}

//MinusOperator Operator 的实际减法实现
// MinusOperator Operator 的实际减法实现
type MinusOperator struct {
*OperatorBase
}

//Result 获取结果
// Result 获取结果
func (o MinusOperator) Result() int {
return o.a - o.b
}
3 changes: 1 addition & 2 deletions 05_abstract_factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

如果抽象工厂退化成生成的对象无关联则成为工厂函数模式。

比如本例子中使用RDB和XML存储订单信息,抽象工厂分别能生成相关的主订单信息和订单详情信息。
如果业务逻辑中需要替换使用的时候只需要改动工厂函数相关的类就能替换使用不同的存储方式了。
比如本例子中使用 `RDB``XML` 存储订单信息,抽象工厂分别能生成相关的主订单信息和订单详情信息。 如果业务逻辑中需要替换使用的时候只需要改动工厂函数相关的类就能替换使用不同的存储方式了。
Loading

0 comments on commit ea1f5b3

Please sign in to comment.