设计模式案例(二)

简介: 设计模式案例(二)

系列文章目录


第一章 设计模式案例 (一)

第一章 设计模式案例 (二)


前言


上一篇文章介绍了常用的几种设计模式和常用场景,便于对设计模式加深理解,此文章主要讲解设计模式的案例。


一、责任链模式


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()
}
目录
相关文章
|
1天前
|
设计模式 算法 搜索推荐
Head First设计模式中的典型设计模式解析与案例分析
Head First设计模式中的典型设计模式解析与案例分析
|
6天前
|
设计模式 算法 Java
Java中的设计模式:实战案例分享
Java中的设计模式:实战案例分享
|
2月前
|
设计模式 Java 关系型数据库
Java设计模式--创建模式工厂--用披萨订购案例 详细讲解三种工厂模式
Java设计模式--创建模式工厂--用披萨订购案例 详细讲解三种工厂模式
|
2月前
|
设计模式 存储 安全
NFT佛萨奇矩阵公排合约系统开发|案例分析|设计模式
区块链目前面临的另一个挑战是可扩展性问题
|
2月前
|
设计模式
设计模式案例 (三)
设计模式案例 (三)
26 0
|
2月前
|
设计模式
设计模式案例(一)
设计模式案例(一)
26 0
|
9月前
|
设计模式 Java 应用服务中间件
【设计模式——学习笔记】23种设计模式——职责链/责任链模式(Chain of Responsibility)(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——职责链/责任链模式(Chain of Responsibility)(原理讲解+应用场景介绍+案例介绍+Java代码实现)
97 0
|
9月前
|
设计模式 算法 Java
【设计模式——学习笔记】23种设计模式——策略模式Strategy(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——策略模式Strategy(原理讲解+应用场景介绍+案例介绍+Java代码实现)
74 0
|
9月前
|
设计模式 Java 开发者
【设计模式——学习笔记】23种设计模式——状态模式State(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——状态模式State(原理讲解+应用场景介绍+案例介绍+Java代码实现)
54 0
|
9月前
|
设计模式 存储 自然语言处理
【设计模式——学习笔记】23种设计模式——解释器模式Interpreter(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——解释器模式Interpreter(原理讲解+应用场景介绍+案例介绍+Java代码实现)
41 0