系列文章目录
第一章 设计模式案例 (一)
第一章 设计模式案例 (二)
前言
上一篇文章介绍了常用的几种设计模式和常用场景,便于对设计模式加深理解,此文章主要讲解设计模式的案例。
一、责任链模式
case 包
代码如下(示例):
package _case import "fmt" func DutyCase() { pm := &ProjectManager{} dm := &DepartmentManager{} gm := &GeneralManager{} pm.SetNext(dm).SetNext(gm) pm.Handle("张三", 300) pm.Handle("李四", 800) pm.Handle("王五", 1800) } type Handler interface { SetNext(h Handler) Handler Handle(user string, amount int64) } // 项目经理 type ProjectManager struct { next Handler } func (pm *ProjectManager) Handle(user string, amount int64) { if amount <= 500 { fmt.Printf("%s 申请聚餐费用 %d 元,项目经理审批通过\n", user, amount) return } // 项目经理没有权限,交给下一个人处理 pm.next.Handle(user, amount) } func (pm *ProjectManager) SetNext(h Handler) Handler { pm.next = h return h } // 部门经理 type DepartmentManager struct { next Handler } func (dm *DepartmentManager) Handle(user string, amount int64) { if amount > 500 && amount <= 1000 { fmt.Printf("%s 申请聚餐费用 %d 元,部门经理审批通过\n", user, amount) return } else if amount > 1000 { fmt.Printf("%s 申请聚餐费用 %d 元,部门经理审批通过,总经理加签\n", user, amount) dm.next.Handle(user, amount) } else { dm.next.Handle(user, amount) } } func (dm *DepartmentManager) SetNext(h Handler) Handler { dm.next = h return h } // 总经理 type GeneralManager struct { next Handler } func (gm *GeneralManager) Handle(user string, amount int64) { if amount > 1000 { fmt.Printf("%s 申请聚餐费用 %d 元,总经理审批通过\n", user, amount) return } gm.next.Handle(user, amount) } func (gm *GeneralManager) SetNext(h Handler) Handler { gm.next = h return h }
代码如下(示例):main
package main import _case "design-pattern/duty/case" func main() { _case.DutyCase() }
二、策略模式
case 包
代码如下(示例):
package _case import "fmt" func StrategyCase() { arr := []int{1, 3, 8, 2, 4, 5} arr1 := []int{5, 2, 4, 6, 9, 8} ctx := &Context{} //设置排序策略- 冒泡算法 ctx.SetStrategy(&bubble{}) ctx.ExecuteStrategy(arr) fmt.Println(arr) //设置排序策略-选择策略 ctx.SetStrategy(&selection{}) ctx.ExecuteStrategy(arr1) fmt.Println(arr1) } // 排序策略定义 type SortStrategy interface { Sort(arr []int) } // 冒泡排序 type bubble struct{} func (*bubble) Sort(arr []int) { fmt.Println("冒泡算法") n := len(arr) for i := 0; i < n-1; i++ { flag := false for j := 0; j < n-1-i; j++ { if arr[j] > arr[j+1] { arr[j], arr[j+1] = arr[j+1], arr[j] flag = true } } if !flag { break } } } // 选择排序 type selection struct{} func (*selection) Sort(arr []int) { fmt.Println("选择排序") size := len(arr) for i := 0; i < size-1; i++ { minIndex := i for j := i + 1; j < size; j++ { if arr[j] < arr[minIndex] { minIndex = j } } if minIndex != i { arr[i], arr[minIndex] = arr[minIndex], arr[i] } } } type Context struct { stratety SortStrategy } func (ctx *Context) SetStrategy(s SortStrategy) { ctx.stratety = s } func (ctx *Context) ExecuteStrategy(arr []int) { ctx.stratety.Sort(arr) }
代码如下(示例):main
package main import _case "design-pattern/trategy/case" func main() { _case.StrategyCase() } }
三、工厂模式
case 包
代码如下(示例):简单工厂
package _case type Product interface { GetName() string } type ProductA struct { name string } func (a *ProductA) GetName() string { return a.name } type ProductB struct { name string } func (b *ProductB) GetName() string { return b.name } type ProductC struct { name string } func (c *ProductC) GetName() string { return c.name }
package _case import ( "fmt" ) type SimpleFactory struct { } func (f *SimpleFactory) CreateProduct(productType string) Product { switch productType { case "A": return &ProductA{ name: "product A", } case "B": return &ProductB{ name: "product B", } case "C": return &ProductC{ name: "product C", } } return nil } func SimpleFactoryCase() { factory := &SimpleFactory{} var product Product product = factory.CreateProduct("A") fmt.Println(product.GetName()) product = factory.CreateProduct("B") fmt.Println(product.GetName()) product = factory.CreateProduct("C") if product != nil { fmt.Println(product.GetName()) } }
代码如下(示例):简单工厂
package _case type Product interface { GetName() string } type ProductA struct { name string } func (a *ProductA) GetName() string { return a.name } type ProductB struct { name string } func (b *ProductB) GetName() string { return b.name } type ProductC struct { name string } func (c *ProductC) GetName() string { return c.name }
package _case import "fmt" type Factory interface { CreateProduct() Product } type FactoryA struct { } func (f *FactoryA) CreateProduct() Product { return &ProductA{ name: "product A", } } type FactoryB struct { } func (f *FactoryB) CreateProduct() Product { return &ProductB{ name: "product B", } } type FactoryC struct { } func (f *FactoryC) CreateProduct() Product { return &ProductC{ name: "product C", } } func FactoryMethodCase() { var factory Factory var product Product factory = &FactoryA{} product = factory.CreateProduct() fmt.Println(product.GetName()) factory = &FactoryB{} product = factory.CreateProduct() fmt.Println(product.GetName()) factory = &FactoryC{} product = factory.CreateProduct() fmt.Println(product.GetName()) }
代码如下(示例):main
package main import _case "design-pattern/factory/case" func main() { //_case.SimpleFactoryCase() _case.FactoryMethodCase() }