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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
S
Securelist
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tor Project blog
S
Schneier on Security
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
AWS News Blog
AWS News Blog
F
Fortinet All Blogs
C
Cisco Blogs
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
月光博客
月光博客
量子位
A
Arctic Wolf
博客园 - 叶小钗
L
LINUX DO - 热门话题
J
Java Code Geeks
The Hacker News
The Hacker News
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
T
Tenable Blog
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
Simon Willison's Weblog
Simon Willison's Weblog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threat Research - Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Y
Y Combinator Blog
PCI Perspectives
PCI Perspectives
Hacker News: Ask HN
Hacker News: Ask HN
P
Palo Alto Networks Blog
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
O
OpenAI News
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog
N
Netflix TechBlog - Medium

博客园 - ®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: Producer Consumer  Pattern python: Producer Consumer Pattern go: Parallelism Pattern python: Parallelism Pattern go: Reactor Pattern python: Reactor Pattern go: Generators Pattern python: speech to text offline python: Generators Pattern go: Coroutines Pattern python:Coroutines Pattern go: Broadcast Pattern python: Broadcast Pattern python: Bounded Parallelism Pattern go: Bounded Parallelism Pattern python: N-Barrier Pattern go: N-Barrier Pattern python: Semaphore Pattern go: Semaphore Pattern python: Read-Write Lock Pattern go: Read-Write Lock Pattern python: Monitor Pattern go: Monitor Pattern python: Mutex Pattern go: Lock/Mutex Pattern Python: Condition Variable Pattern go:Condition Variable Pattern python: Registry Pattern python: Interpreter Pattern go: Interpreter Pattern go: Registry Pattern go: Memento Pattern go: Command Pattern go: State Pattern go:Template Method Pattern go: Strategy Pattern go: Visitor Pattern go: Mediator Pattern go: Iterator Pattern go: Chain of Responsibility Pattern go: Proxy Pattern go:Decorator Pattern go: Facade Pattern go: Flyweight Pattern go: Composite Pattern go: Singleton Pattern go: Prototype Pattern go: Bridge Pattern go: Adapter Pattern go: Builder Pattern 密码进行加盐哈希 using CSharp,Python,Go,Java go: Abstract Factory Pattern go: Model,Interface,DAL ,Factory,BLL using mysql go: Simple Factory Pattern go: Factory Method Pattern go: goLang 在Windows环境搭建Go语言开发环境 CSharp: Parallel Extensions cpp: class python: 蝴蝶跟随鼠标 javascript: 中国历史人物热力分布图using echart javascript: 中国历史人物热力图 python: 初养龙虾微信纯文字自动回复using workBuddy python: object 入门 python: Factory Method Pattern python: Simple Factory Pattern python: Abstract Factory Pattern 区域化 代码 python: Null Object Pattern python: Builder Pattern python: Adapter Pattern
go: Observer Pattern
®Geovin Du D · 2026-04-22 · via 博客园 - ®Geovin Du Dream Park™

项目结构:

image

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:48
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : observer.go
*/
package observer

// Observer 观察者接口
type Observer interface {
	Update(message string)
	GetName() string
}

// Subject 主题接口
type Subject interface {
	Register(observer Observer)
	UnRegister(observer Observer)
	Notify(message string)
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : jewelry_store.go
*/
package subject

import (
	"fmt"
	"godesginpattern/observer/observer"
	"sync"
)

type JewelryStore struct {
	name      string
	observers []observer.Observer
	mu        sync.RWMutex
}

func NewJewelryStore(name string) *JewelryStore {
	return &JewelryStore{
		name:      name,
		observers: make([]observer.Observer, 0),
	}
}

func (j *JewelryStore) Register(observer observer.Observer) {
	j.mu.Lock()
	defer j.mu.Unlock()
	j.observers = append(j.observers, observer)
	fmt.Printf("【%s】新关注者:%s\n", j.name, observer.GetName())
}

func (j *JewelryStore) UnRegister(observer observer.Observer) {
	j.mu.Lock()
	defer j.mu.Unlock()

	for i, obs := range j.observers {
		if obs.GetName() == observer.GetName() {
			j.observers = append(j.observers[:i], j.observers[i+1:]...)
			fmt.Printf("【%s】取消关注者:%s\n", j.name, observer.GetName())
			break
		}
	}
}

func (j *JewelryStore) Notify(message string) {
	j.mu.RLock()
	defer j.mu.RUnlock()

	fmt.Printf("\n【%s】发布新动态:%s\n", j.name, message)
	for _, observer := range j.observers {
		observer.Update(message)
	}
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : lover.go
*/
package entity

import "fmt"

type Lover struct {
	name string
}

func NewLover(name string) *Lover {
	return &Lover{name: name}
}

func (l *Lover) Update(message string) {
	fmt.Printf("📣 珠宝爱好者【%s】收到通知:%s → 我要去看看新款!\n", l.name, message)
}

func (l *Lover) GetName() string {
	return l.name
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : collector.go
*/
package entity

import "fmt"

type Collector struct {
	name string
}

func NewCollector(name string) *Collector {
	return &Collector{name: name}
}

func (c *Collector) Update(message string) {
	fmt.Printf("📣 珠宝收藏家【%s】收到通知:%s → 稀缺款我要立刻收藏!\n", c.name, message)
}

func (c *Collector) GetName() string {
	return c.name
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : dealer.go
*/
package entity

import "fmt"

type Dealer struct {
	name string
}

func NewDealer(name string) *Dealer {
	return &Dealer{name: name}
}

func (d *Dealer) Update(message string) {
	fmt.Printf("📣 珠宝经销商【%s】收到通知:%s → 准备批量采购!\n", d.name, message)
}

func (d *Dealer) GetName() string {
	return d.name
}
/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:49
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : notify_service.go
*/
package service

import (
	"godesginpattern/observer/observer"
	"godesginpattern/observer/subject"
)

type NotifyService struct {
	store *subject.JewelryStore
}

func NewNotifyService(store *subject.JewelryStore) *NotifyService {
	return &NotifyService{store: store}
}

func (s *NotifyService) Register(observer observer.Observer) {
	s.store.Register(observer)
}

func (s *NotifyService) UnRegister(observer observer.Observer) {
	s.store.UnRegister(observer)
}

func (s *NotifyService) Notify(message string) {
	s.store.Notify(message)
}

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Observer 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/4/22 22:56
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : observerbll.go
observer/
├── go.mod                  # 项目模块定义
├── internal/               # 内部包(外部不可引用)
│   ├── observer/           # 观察者核心层(接口定义)
│   │   └── observer.go     # Observer/Subject 接口
│   ├── subject/            # 主题实现层(珠宝店)
│   │   └── jewelry_store.go
│   ├── entity/             # 实体层(观察者角色)
│   │   ├── lover.go
│   │   ├── collector.go
│   │   └── dealer.go
│   └── service/            # 业务逻辑层
│       └── notify_service.go
└── main.go                 # 程序入口

分层职责说明
observer(抽象层):定义接口,不依赖任何具体实现
subject(主题层):实现珠宝店主题,管理观察者
entity(实体层):实现各类观察者角色,纯业务响应
service(业务层):封装通知逻辑,解耦入口与实现
main.go(入口层):依赖注入、流程编排
*/
package bll

import (
	"godesginpattern/observer/entity"
	"godesginpattern/observer/service"
	"godesginpattern/observer/subject"
)

func ObserverMain() {
	// 1. 创建珠宝店
	store := subject.NewJewelryStore("璀璨珠宝旗舰店")

	// 2. 初始化服务
	notifySrv := service.NewNotifyService(store)

	// 3. 创建观察者
	lover := entity.NewLover("张三")
	collector := entity.NewCollector("李大爷")
	dealer := entity.NewDealer("王总")

	// 4. 注册关注
	notifySrv.Register(lover)
	notifySrv.Register(collector)
	notifySrv.Register(dealer)

	// 5. 新品通知
	notifySrv.Notify("新款红宝石项链到货,限量50件!")

	// 6. 取消关注
	notifySrv.UnRegister(lover)

	// 7. 打折通知
	notifySrv.Notify("全场珠宝8折优惠,限时3天!")
}

输出:

image

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