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

推荐订阅源

D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
B
Blog
博客园 - Franky
I
InfoQ
A
About on SuperTechFans
博客园_首页
L
LangChain Blog
量子位
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
美团技术团队
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
月光博客
月光博客
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
G
Google Developers Blog
Last Week in AI
Last Week in AI

博客园 - ®Geovin Du Dream Park™

go: Functional Options Pattern go:Timing Functions Pattern python: Timing Functions Pattern go: Steady-State Pattern python: Steady-State Pattern go: Handshaking Pattern python: Handshaking Pattern go: Fail-Fast Pattern python: Fail-Fast Pattern I go: Deadline Pattern python: Deadline Pattern go: Circuit-Breaker Pattern python: Circuit-Breaker Pattern go: Bulkheads Pattern python: Bulkheads Pattern go: Push & Pull Pattern python: Push & Pull Pattern python: Publish/Subscribe Pattern II go: Publish/Subscribe Pattern python: Publish/Subscribe Pattern go: Futures & Promises Pattern python: Futures & Promises Pattern go: Worker Pool Pattern go:Pipeline Pattern python: Worker Pool Pattern python: Pipeline Pattern go: Fan-Out Pattern python: Fan-Out Pattern go: Fan-In Pattern python: Fan-In Pattern Fan-In
go: Reactor Pattern
®Geovin Du Dream Park™ · 2026-06-16 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Reactor  Pattern 反应器模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/16 20:22
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : common.go
*/
package common

import (
	"log"
	"os"
)

// 业务事件类型枚举
type BusinessEventType string

const (
	RAW_MATERIAL      BusinessEventType = "raw_material"
	PROCESS_CHECK     BusinessEventType = "process_check"
	STORE_SALE        BusinessEventType = "sale"
	AFTER_SALE_REPAIR BusinessEventType = "repair"
	INVENTORY_CHECK   BusinessEventType = "inventory"
)

// 统一日志
func GetReactorLogger(name string) *log.Logger {
	return log.New(os.Stdout, "["+name+"] ", log.Ldate|log.Ltime)
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Reactor  Pattern 反应器模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/16 20:22
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : event.go
*/
package event

import "godesginpattern/reactor/common"

// 业务事件实体(对齐 Python dataclass)
type BusinessEvent struct {
	EventType common.BusinessEventType
	Payload   map[string]interface{}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Reactor  Pattern 反应器模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/16 20:23
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : handler.go
*/
package handler

import (
	"godesginpattern/reactor/common"
	"godesginpattern/reactor/event"
)

var logger = common.GetReactorLogger("handler")

// 原料采购
type MaterialHandler struct{}

func (h *MaterialHandler) Handle(e event.BusinessEvent) {
	p := e.Payload
	logger.Printf("【原料采购验收】原料:%s | 纯度:%s | 重量:%vg",
		p["material_type"], p["purity"], p["weight"])
	logger.Printf("%s 原料验收合格,入库完成!", p["material_type"])
}

// 加工质检
type ProcessHandler struct{}

func (h *ProcessHandler) Handle(e event.BusinessEvent) {
	p := e.Payload
	logger.Printf("【珠宝加工质检】款式:%s | 证书:%s | 工艺:%s",
		p["style"], p["cert_no"], p["craft"])
	logger.Printf("%s 工艺合格,证书合规,成品入库!", p["style"])
}

// 销售结算
type SaleHandler struct{}

func (h *SaleHandler) Handle(e event.BusinessEvent) {
	p := e.Payload
	price := p["unit_price"].(int)
	qty := p["quantity"].(int)
	total := price * qty
	logger.Printf("【门店销售结算】商品:%s | 单价:%v元 | 数量:%v件",
		p["product_name"], price, qty)
	logger.Printf("订单%s 结算总价:%v元,销售完成!", p["order_id"], total)
}

// 售后维修
type RepairHandler struct{}

func (h *RepairHandler) Handle(e event.BusinessEvent) {
	p := e.Payload
	logger.Printf("【售后维修】客户:%s | 饰品:%s | 项目:%s",
		p["customer_name"], p["jewelry_name"], p["repair_item"])
	logger.Printf("%s 完成,可领取珠宝!", p["repair_item"])
}

// 库存盘点
type InventoryHandler struct{}

func (h *InventoryHandler) Handle(e event.BusinessEvent) {
	p := e.Payload
	logger.Printf("【库存盘点】类型:%s | 总数:%v | 总价值:%v万元",
		p["inventory_type"], p["total_num"], p["total_value"])
	logger.Println("盘点结果:账实相符,库存更新完成!")
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Reactor  Pattern 反应器模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/16 20:24
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : reactor.go
*/
package reactors

import (
	"godesginpattern/reactor/common"
	"godesginpattern/reactor/event"
	"godesginpattern/reactor/handler"
)

// 事件分离器 Demultiplexer
type EventDemultiplexer struct {
	handlerMap map[common.BusinessEventType]interface{ Handle(e event.BusinessEvent) }
}

func NewDemux() *EventDemultiplexer {
	return &EventDemultiplexer{
		handlerMap: make(map[common.BusinessEventType]interface{ Handle(e event.BusinessEvent) }),
	}
}
func (d *EventDemultiplexer) Register(t common.BusinessEventType, h interface{ Handle(e event.BusinessEvent) }) {
	d.handlerMap[t] = h
}
func (d *EventDemultiplexer) Get(t common.BusinessEventType) interface{ Handle(e event.BusinessEvent) } {
	return d.handlerMap[t]
}

// Reactor 核心
type JewelryReactor struct {
	demux *EventDemultiplexer
}

func NewJewelryReactor() *JewelryReactor {
	demux := NewDemux()
	demux.Register(common.RAW_MATERIAL, &handler.MaterialHandler{})
	demux.Register(common.PROCESS_CHECK, &handler.ProcessHandler{})
	demux.Register(common.STORE_SALE, &handler.SaleHandler{})
	demux.Register(common.AFTER_SALE_REPAIR, &handler.RepairHandler{})
	demux.Register(common.INVENTORY_CHECK, &handler.InventoryHandler{})

	return &JewelryReactor{demux: demux}
}

func (r *JewelryReactor) Dispatch(e event.BusinessEvent) {
	h := r.demux.Get(e.EventType)
	if h != nil {
		h.Handle(e)
	}
}

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/6/16 20:25
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : reactorbll.go
*/
package bll

import (
	"godesginpattern/reactor/common"
	"godesginpattern/reactor/event"
	"godesginpattern/reactor/reactors"
)

var reactorlogger = common.GetReactorLogger("ReactorBll")

// ReactorBll 你要的封装类
type ReactorBll struct {
	reactor *reactors.JewelryReactor
}

func NewReactorBll() *ReactorBll {
	return &ReactorBll{
		reactor: reactors.NewJewelryReactor(),
	}
}

func ReactorMain(b *ReactorBll) {
	reactorlogger.Println("========== ReactorBll demo 开始 ==========")

	events := []event.BusinessEvent{
		{
			EventType: common.RAW_MATERIAL,
			Payload: map[string]interface{}{
				"material_type": "足金999",
				"purity":        "99.9%",
				"weight":        800,
			},
		},
		{
			EventType: common.PROCESS_CHECK,
			Payload: map[string]interface{}{
				"style":   "1克拉钻戒",
				"cert_no": "GIA987654321",
				"craft":   "微镶精工",
			},
		},
		{
			EventType: common.STORE_SALE,
			Payload: map[string]interface{}{
				"product_name": "古法金镯",
				"unit_price":   15680,
				"quantity":     1,
				"order_id":     "JEW20260615001",
			},
		},
		{
			EventType: common.AFTER_SALE_REPAIR,
			Payload: map[string]interface{}{
				"customer_name": "李女士",
				"jewelry_name":  "18K金项链",
				"repair_item":   "焊接+抛光",
			},
		},
		{
			EventType: common.INVENTORY_CHECK,
			Payload: map[string]interface{}{
				"inventory_type": "钻石成品",
				"total_num":      1680,
				"total_value":    12600,
			},
		},
	}

	for _, evt := range events {
		b.reactor.Dispatch(evt)
	}

	reactorlogger.Println("========== ReactorBll demo 结束 ==========")
}

输出:

image

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)