惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
WordPress大学
WordPress大学
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
Jina AI
Jina AI
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
D
Docker

Mohuishou

如何实现支持多集群的 Kubernetes Operator? 第三方应用如何调用我们 kubebuilder 生成的自定义资源? Kubernetes 简明教程 k8s job 为何迟迟不能结束? Go 工程化(十一) 如何优雅的写出 repo 层代码 Go 工程化(十) 如何在整洁架构中使用事务? 给博客添加章节目录 使用 Notion Database 管理静态博客文章 一个普通 Go 开发的三年 4. localhost 就一定是 localhost 么? Go可用性(七) 总结: 一张图串联可用性知识点 Go可用性(六) 熔断 10. 总结 9. kubebuilder 进阶: 源码分析 8. kubebuilder 进阶: webhook 7. kubebuilder 进阶: 测试 6. kubebuilder 实战: status & event 5. kubebuilder 实战: CRUD 4. kustomize 简明教程 3. KubeBuilder 简明教程 2. Kind: 如何快速搭建本地 K8s 开发环境? 1. Operator概述: 如何对 Kubernetes 进行扩展 Go可用性(五) 自适应限流 Go可用性(四) 漏桶算法 Go可用性(三) 令牌桶的实现 rate/limt Go可用性(二) 令牌桶原理及使用 Go可用性(一) 隔离设计 Go并发编程(十二) Singleflight Go工程化(九) 项目重构实践 Go工程化(八) 单元测试
Go设计模式21-命令模式
Mohuishou · 2020-10-29 · via Mohuishou

注:本文已发布超过一年,请注意您所使用工具的相关版本是否适用

笔记

代码实现

接下来会有两个例子,第一个是按照原文定义的方式,将函数封装成对象,第二个例子我们直接将函数作为参数传递。

将函数封装为对象

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Package command 命令模式
// Blog: https://lailin.xyz/post/command.html
// 这是示例一,采用将函数封装为对象的方式实现,
// 示例说明:
// 假设现在有一个游戏服务,我们正在实现一个游戏后端
// 使用一个 goroutine 不断接收来自客户端请求的命令,并且将它放置到一个队列当中
// 然后我们在另外一个 goroutine 中来执行它
package command

import "fmt"

// ICommand 命令
type ICommand interface {
Execute() error
}

// StartCommand 游戏开始运行
type StartCommand struct{}

// NewStartCommand NewStartCommand
func NewStartCommand( /*正常情况下这里会有一些参数*/ ) *StartCommand {
return &StartCommand{}
}

// Execute Execute
func (c *StartCommand) Execute() error {
fmt.Println("game start")
return nil
}

// ArchiveCommand 游戏存档
type ArchiveCommand struct{}

// NewArchiveCommand NewArchiveCommand
func NewArchiveCommand( /*正常情况下这里会有一些参数*/ ) *ArchiveCommand {
return &ArchiveCommand{}
}

// Execute Execute
func (c *ArchiveCommand) Execute() error {
fmt.Println("game archive")
return nil
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package command

import (
"fmt"
"testing"
"time"
)

func TestDemo(t *testing.T) {
// 用于测试,模拟来自客户端的事件
eventChan := make(chan string)
go func() {
events := []string{"start", "archive", "start", "archive", "start", "start"}
for _, e := range events {
eventChan <- e
}
}()
defer close(eventChan)

// 使用命令队列缓存命令
commands := make(chan ICommand, 1000)
defer close(commands)

go func() {
for {
// 从请求或者其他地方获取相关事件参数
event, ok := <-eventChan
if !ok {
return
}

var command ICommand
switch event {
case "start":
command = NewStartCommand()
case "archive":
command = NewArchiveCommand()
}

// 将命令入队
commands <- command
}
}()

for {
select {
case c := <-commands:
c.Execute()
case <-time.After(1 * time.Second):
fmt.Println("timeout 1s")
return
}
}
}

将函数直接作为参数

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Package command 命令模式
// Blog: https://lailin.xyz/post/command.html
// 这是示例二,采用将直接返回一个函数,不用对象
// 示例说明:
// 假设现在有一个游戏服务,我们正在实现一个游戏后端
// 使用一个 goroutine 不断接收来自客户端请求的命令,并且将它放置到一个队列当中
// 然后我们在另外一个 goroutine 中来执行它
package command

import "fmt"

// Command 命令
type Command func() error

// StartCommandFunc 返回一个 Command 命令
// 是因为正常情况下不会是这么简单的函数
// 一般都会有一些参数
func StartCommandFunc() Command {
return func() error {
fmt.Println("game start")
return nil
}
}

// ArchiveCommandFunc ArchiveCommandFunc
func ArchiveCommandFunc() Command {
return func() error {
fmt.Println("game archive")
return nil
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package command

import (
"fmt"
"testing"
"time"
)

func TestDemoFunc(t *testing.T) {
// 用于测试,模拟来自客户端的事件
eventChan := make(chan string)
go func() {
events := []string{"start", "archive", "start", "archive", "start", "start"}
for _, e := range events {
eventChan <- e
}

}()
defer close(eventChan)

// 使用命令队列缓存命令
commands := make(chan Command, 1000)
defer close(commands)

go func() {
for {
// 从请求或者其他地方获取相关事件参数
event, ok := <-eventChan
if !ok {
return
}

var command Command
switch event {
case "start":
command = StartCommandFunc()
case "archive":
command = ArchiveCommandFunc()
}

// 将命令入队
commands <- command
}
}()

for {
select {
case c := <-commands:
c()
case <-time.After(1 * time.Second):
fmt.Println("timeout 1s")
return
}
}
}

关注我获取更新

猜你喜欢