设计模式案例(一)

系列文章目录

例如:第一章 设计模式案例



前言

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


一、适配器模式

case 包

代码如下(示例):

package _case

import "fmt"

func AdapterCase() {
	var cache StdCache
	redis := &Redis{data: map[string]string{}}
	cache = &RedisAdapter{redis: redis}
	cache.Set("key1", "value1")
	cache.Set("key2", "value2")
	cache.Set("key3", "value3")
	fmt.Println(cache.Get("key1"))
	fmt.Println(cache.Get("key2"))
	fmt.Println(cache.Get("key3"))

	mem := &MemCache{data: map[string]interface{}{}}
	cache = &MemCacheAdapter{mem: mem}
	cache.Set("k1", "v1")
	cache.Set("k2", "v2")
	cache.Set("k3", "v3")
	fmt.Println(cache.Get("k1"))
	fmt.Println(cache.Get("k2"))
	fmt.Println(cache.Get("k3"))

}

type Redis struct {
	data map[string]string
}

func (r *Redis) GetStr(key string) string {
	return r.data[key]
}

func (r *Redis) SetStr(key, value string) {
	r.data[key] = value
}

type MemCache struct {
	data map[string]interface{}
}

func (m *MemCache) GetItem(key string) interface{} {
	return m.data[key]
}

func (m *MemCache) SetItem(key string, value interface{}) {
	m.data[key] = value
}

// 定义标准缓存
type StdCache interface {
	Get(key string) string
	Set(key, value string)
}

// 定义Redis适配器
type RedisAdapter struct {
	redis *Redis
}

func (adapter *RedisAdapter) Get(key string) string {
	return adapter.redis.GetStr(key)
}

func (adapter *RedisAdapter) Set(key, value string) {
	adapter.redis.SetStr(key, value)
}

// 定义MemCache适配器
type MemCacheAdapter struct {
	mem *MemCache
}

func (adapter *MemCacheAdapter) Get(key string) string {
	return adapter.mem.GetItem(key).(string)
}

func (adapter *MemCacheAdapter) Set(key, value string) {
	adapter.mem.SetItem(key, value)
}

代码如下(示例):main

package main

import _case "adapter/case"

func main() {
	_case.AdapterCase()
}

二、观察者模式

case 包

代码如下(示例):case 包

package _case

import (
	"fmt"
)

func ObserverCase() {
	var ob Observer = &WatchConf{}
	var ob1 Observer = &WatchConf{}
	var ob2 Observer = &WatchConf{}
	var ob3 Observer = &WatchConf{}
	var pb Publisher = &Config{data: map[string]string{"host": "localhost", "port": "5051"}}

	//订阅
	pb.Subscribe(ob)
	pb.Subscribe(ob1)
	pb.Subscribe(ob2)
	pb.Subscribe(ob3)

	fmt.Println(pb)

	pb.UnSubscribe(ob1)
	fmt.Println(pb)

	conf := pb.(*Config) // 断言 判断pb 类型
	conf.data = map[string]string{"host": "127.0.0.1", "port": "9998"}
	pb.NotityObserver(conf.data)
	fmt.Println(pb)
}

// 定义观察者接口
type Observer interface {
	Update(data interface{})
}

// 定义发布者
type Publisher interface {
	//关注
	Subscribe(observer Observer)
	//取关
	UnSubscribe(observer Observer)
	//通知
	NotityObserver(data interface{})
}

// 定义具体发布者
type Config struct {
	data      map[string]string
	Observers []Observer
}

// 关注
func (c *Config) Subscribe(o Observer) {
	c.Observers = append(c.Observers, o)
}

// 取关
func (c *Config) UnSubscribe(o Observer) {
	for i, v := range c.Observers {
		if v == o {
			c.Observers = append(c.Observers[:i], c.Observers[i+1:]...)
			break
		}
	}

}

// 通知观察者
func (c *Config) NotityObserver(data interface{}) {
	for _, ob := range c.Observers {
		ob.Update(data)
	}
}

type WatchConf struct {
}

func (*WatchConf) Update(data interface{}) {
	fmt.Println("受到配置更新消息,更新配置为:", data)
}

代码如下(示例):main

package main

import _case "observer/case"

func main() {
	_case.ObserverCase()
}

三、代理模式

case 包

代码如下(示例):case 包

package _case

import "fmt"

func ProxyCase() {
	var cache Icache
	cache = &Cache{data: map[string]interface{}{}}

	proxy := NewProxy(cache)

	proxy.Set("key1", "value1")
	proxy.Set("key2", "value2")
	proxy.Set("key3", "value3")
	proxy.Set("key4", "value4")
	fmt.Println(proxy.Get("key1"))
	fmt.Println(proxy.Get("key2"))
	fmt.Println(proxy.Get("key3"))
	fmt.Println(proxy.Get("key4"))

}

type Icache interface {
	Get(key string) interface{}
	Set(key string, value interface{})
}

// 被代理对象(真实对象)
type Cache struct {
	data map[string]interface{}
}

func (c *Cache) Get(key string) interface{} {
	return c.data[key]
}

func (c *Cache) Set(key string, value interface{}) {
	c.data[key] = value
}

// 代理对象
type Proxy struct {
	cache Icache
}

func NewProxy(cache Icache) *Proxy {
	return &Proxy{
		cache: cache,
	}
}

func (p *Proxy) Get(key string) interface{} {
	// 此处可以增加访问控制逻辑等扩展功能
	return p.cache.Get(key)
}

func (p *Proxy) Set(key string, value interface{}) {
	// 此处可以增加访问控制逻辑等扩展功能
	p.cache.Set(key, value)
}

代码如下(示例):main

package main

import _case "design-pattern/proxy/case"

func main() {
	_case.ProxyCase()
}

总结

提示:这里对文章进行总结:

以上就是今天要讲的内容,本文暂时讲解了部分设计模式案例,后期会在此处持续更新

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值