更新各个设计模式示例,添加工厂方法、原型和单例模式的实现,重构相关代码并增强可读性
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
实例
|
||||
小汤有一家披萨店,他想通过工厂方法模式来创建不同类型的披萨。
|
||||
首先,他定义了一个披萨接口和具体的披萨类,然后创建了一个披萨工厂接口和具体的披萨工厂类来生产不同类型的披萨。
|
||||
*/
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 披萨接口
|
||||
type Pizza interface {
|
||||
show()
|
||||
}
|
||||
|
||||
// 披萨工厂接口
|
||||
type PizzaFactory interface {
|
||||
createPizza() Pizza
|
||||
}
|
||||
|
||||
// 具体的披萨类
|
||||
type CheesePizza struct{}
|
||||
|
||||
func (p *CheesePizza) show() {
|
||||
fmt.Println(" 这是个芝士披萨!")
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
type PepperoniPizza struct{}
|
||||
|
||||
func (p *PepperoniPizza) show() {
|
||||
fmt.Println(" 这是个意大利辣香肠披萨!")
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// 具体的披萨工厂类
|
||||
type CheesePizzaFactory struct{}
|
||||
|
||||
func (f *CheesePizzaFactory) createPizza() Pizza {
|
||||
return &CheesePizza{}
|
||||
}
|
||||
|
||||
type PepperoniPizzaFactory struct{}
|
||||
|
||||
func (f *PepperoniPizzaFactory) createPizza() Pizza {
|
||||
return &PepperoniPizza{}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var factory PizzaFactory
|
||||
var pizza Pizza
|
||||
|
||||
fmt.Println("========= 工厂方法模式 =========")
|
||||
|
||||
// 创建芝士披萨
|
||||
factory = &CheesePizzaFactory{}
|
||||
fmt.Println("芝士披萨工厂启动:")
|
||||
pizza = factory.createPizza()
|
||||
pizza.show()
|
||||
|
||||
// 创建意大利辣香肠披萨
|
||||
factory = &PepperoniPizzaFactory{}
|
||||
fmt.Println("意大利辣香肠披萨工厂启动:")
|
||||
pizza = factory.createPizza()
|
||||
pizza.show()
|
||||
|
||||
fmt.Println("======= 工厂方法模式结束 =======")
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
实例
|
||||
汤宝宝穿越到了星球大战世界,并掉到了克隆人军队的生产工厂。
|
||||
|
||||
ps:
|
||||
go中没有官方的深拷贝实现,一些第三方库会使用reflect的方式来实现深拷贝,但性能较差且不够类型安全。
|
||||
比较安全的方法是逐步复制每个字段,会有稍许麻烦。
|
||||
*/
|
||||
|
||||
import "fmt"
|
||||
|
||||
// 克隆人接口
|
||||
type CloneTrooper interface {
|
||||
clone() CloneTrooper
|
||||
fight()
|
||||
setid(id int)
|
||||
}
|
||||
|
||||
// 具体的克隆人类
|
||||
type CloneTrooperA struct {
|
||||
Id int
|
||||
}
|
||||
|
||||
func (c *CloneTrooperA) clone() CloneTrooper {
|
||||
fmt.Printf("克隆人A-%d号正在克隆...\n", c.Id)
|
||||
return &CloneTrooperA{Id: c.Id}
|
||||
}
|
||||
|
||||
func (c *CloneTrooperA) fight() {
|
||||
fmt.Printf("克隆人A-%d号准备战斗!\n", c.Id)
|
||||
}
|
||||
|
||||
func (c *CloneTrooperA) setid(id int) {
|
||||
c.Id = id
|
||||
}
|
||||
|
||||
type CloneTrooperB struct {
|
||||
Id int
|
||||
}
|
||||
|
||||
func (c *CloneTrooperB) clone() CloneTrooper {
|
||||
fmt.Printf("克隆人B-%d号正在克隆...\n", c.Id)
|
||||
return &CloneTrooperB{Id: c.Id}
|
||||
}
|
||||
|
||||
func (c *CloneTrooperB) fight() {
|
||||
fmt.Printf("克隆人B-%d号准备战斗!\n", c.Id)
|
||||
}
|
||||
|
||||
func (c *CloneTrooperB) setid(id int) {
|
||||
c.Id = id
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("========= 原型模式 =========")
|
||||
|
||||
// 创建克隆人A原型
|
||||
prototypeA := &CloneTrooperA{Id: 0}
|
||||
prototypeA.fight()
|
||||
cloneA1 := prototypeA.clone()
|
||||
cloneA1.setid(1)
|
||||
cloneA1.fight()
|
||||
cloneA2 := cloneA1.clone()
|
||||
cloneA2.setid(2)
|
||||
cloneA2.fight()
|
||||
|
||||
// 创建克隆人B原型
|
||||
prototypeB := &CloneTrooperB{Id: 0}
|
||||
prototypeB.fight()
|
||||
cloneB1 := prototypeB.clone()
|
||||
cloneB1.setid(1)
|
||||
cloneB1.fight()
|
||||
cloneB2 := cloneB1.clone()
|
||||
cloneB2.setid(2)
|
||||
cloneB2.fight()
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("======= 原型模式结束 =======")
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
实例
|
||||
汤宝宝混穿到了泰拉世界,并成为了泰拉联邦的唯一统治者。人类只有一个帝皇!
|
||||
*/
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// 帝皇类
|
||||
type Emperor struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
var emperorInstance *Emperor
|
||||
var once sync.Once
|
||||
|
||||
// 获取帝皇实例的函数
|
||||
func GetEmperorInstance(name string, announce string) *Emperor {
|
||||
fmt.Println(announce)
|
||||
once.Do(func() {
|
||||
emperorInstance = &Emperor{Name: name}
|
||||
})
|
||||
return emperorInstance
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Println("========= 单例模式 =========")
|
||||
|
||||
// 获取帝皇实例
|
||||
emperor1 := GetEmperorInstance("汤宝宝", "泰拉联邦的帝皇诞生了!")
|
||||
fmt.Printf("帝皇的名字是:%s!\n", emperor1.Name)
|
||||
emperor2 := GetEmperorInstance("王二狗", "泰拉联邦的帝皇又一次诞生了!")
|
||||
fmt.Printf("帝皇的名字是:%s!\n", emperor2.Name)
|
||||
if emperor1 == emperor2 {
|
||||
fmt.Printf("泰拉只有一个帝皇:%s!\n\n", emperor1.Name)
|
||||
}
|
||||
|
||||
fmt.Println("======= 单例模式结束 =======")
|
||||
}
|
||||
Reference in New Issue
Block a user