• 设计模式案例


    系列文章目录

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



    前言

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


    一、适配器模式

    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)
    }
    
    
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    代码如下(示例):main

    package main
    
    import _case "adapter/case"
    
    func main() {
    	_case.AdapterCase()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    二、观察者模式

    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)
    }
    
    
    • 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
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    代码如下(示例):main

    package main
    
    import _case "observer/case"
    
    func main() {
    	_case.ObserverCase()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    三、代理模式

    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)
    }
    
    • 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
    • 56
    • 57
    • 58
    • 59

    代码如下(示例):main

    package main
    
    import _case "design-pattern/proxy/case"
    
    func main() {
    	_case.ProxyCase()
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    总结

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

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

  • 相关阅读:
    ES-全文搜索
    爬虫实训案例:中国大学排名
    打字速度测试,生成您的打字速度证书?
    vue实现blob文档流下载文件
    应用程序通过 Envoy 代理和 Jaeger 进行分布式追踪 —— Ingress Controller + Http服务 + Grpc服务(三)
    测量网络性能的开源工具iperf3
    PyTorch入门教学——dir()函数和help()函数的应用
    DataX数据同步
    【设计模式】【单例模式】python实现单例模式的几种方式
    光点科技数据口袋数据填报系统满足多类型企业报表需求_光点科技
  • 原文地址:https://blog.csdn.net/sinat_20904903/article/details/134322773