设计模式(二)

四.prototype pattern

一.文档
https://www.jianshu.com/p/42266549e6f9

二.个人理解
原型模式,其实是通过原型来克隆出来相同的、参数一模一样对象的一种设计模式。例如一个图片类,用户调用其画画的功能,画出了复杂的画像,此时希望对该对象进行复制使用,此时可以通过clone的方法,将此对象进行完整克隆,而不需要用户重新再画一遍。

注意此时克隆出来的对象是一个全新的、独立的副本,注意浅拷贝、深拷贝问题。

三.角色
object 对象接口,提供clone接口。

concentrate object 具体对象,实现clone接口

package main
 
import (
    "fmt"
)
 
// 抽象对象接口 支持clone
type object interface {
    Clone() object
    Draw(string)
    Display()
}
 
// 画布类
type canvas struct {
    Content string
}
 
func (c *canvas) Draw(str string) {
    c.Content =  c.Content + str
}
 
func (c *canvas) Display() {
    fmt.Printf("canvas| content[%s].\n", c.Content)
}
 
// 克隆 prototype
func (c *canvas) Clone() object {
    newCanvas := new(canvas)
 
    // 复制内容
    newCanvas.Content = c.Content
 
    return newCanvas
}
 
func main() {
    c := canvas{}
    c.Draw(`  _          _ _     ` + "\n")
    c.Draw(` | |        | | |` + "\n")
    c.Draw(` | |__   ___| | |` + "\n")
    c.Draw(` | '_ \ / _ \ | |/ _ \` + "\n")
    c.Draw(` | | | |  __/ | | (_) |` + "\n")
    c.Draw(` |_| |_|\___|_|_|\___/` + "\n")
    c.Display()
 
    // 克隆
    c2 := c.Clone()
    c2.Display()
 
    return
}
5.singleton pattern

一.文档
https://golang.org/pkg/sync/

https://blog.csdn.net/qibin0506/article/details/50733314

二.个人理解
单例模式顾名思义,类的实例化只会生成一个唯一全局的实例供调用方使用,是非常常用的一种设计模式。在golang中,可以结合sync包非常便捷的实现单例模式。

应用场景:创建对象用于全局使用,有且仅有一个对象。

三.角色
singleton类提供一个getInstance接口,返回其唯一实例

package main
 
import (
    "fmt"
    "time"
    "sync"
)
 
// once提供一次且仅执行一次的调用
var once sync.Once
var product *Product
 
type Product struct {
    createTime int64
}
 
// 获取product实例接口 单例
func getInstance() *Product {
    once.Do(func() {
        product = &Product{
            createTime : time.Now().Unix(),
        }
    })
 
    return product
}
 
func (p *Product) Display() {
    fmt.Printf("Product| create time[%v].\n", p.createTime)
}
 
func main() {
    p := getInstance()
    p.Display()
 
    // 睡眠两秒
    fmt.Println("main| sleep 2 second.")
    time.Sleep(2 * time.Second)
 
    q := getInstance()
    q.Display()
 
    return
}
6.adapter pattern

一.文档
https://refactoring.guru/design-patterns/adapter

二.个人理解
adapter模式,翻译为适配器模式。在开发中,某些接口可能存在不相容的情况,在不改变已有接口的情况下可以通过引入adapter来解决问题,将接口进行转换。现实中,比如旅游出差,不同的国家电压可能不同,而通过电源适配器来进行转换即可。

应用场景:已有接口存在不兼容,通过adapter进行适配

三.角色分类
adapter 适配器类,适配已有接口

service 已有接口、服务

package main
 
import (
    "fmt"
    "strconv"
)
 
type service struct {}
 
func (s *service) Display(data string) {
    fmt.Printf("service| display data[%s].\n", data)
}
 
type adapter struct {
    adaptee service
}
 
func (a *adapter) Display(data int) {
    a.adaptee.Display(strconv.Itoa(data))
}
 
func main() {
    adapter := new(adapter)
    adapter.Display(123)
}
7.facade pattern

一.文档
https://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/facade.html

二.个人理解
facade模式,翻译为外观模式、门面模式,核心思想是屏蔽客户端对复杂系统的认识,通过facade将各个子系统进行整合,对外暴露简单的接口。

应用场景:复杂系统的简单化

三.角色分类
Facade 封装各个子系统

SubSystem 子系统类

package main
 
import (
    "fmt"
    "errors"
)
 
// 设备
// SubSystem 角色
type Equipment struct {}
 
// 检查设备是否正常
// 为简单化 只返回nil
func (e *Equipment) Check() error {
    return nil
}
 
// 线路
// SubSystem 角色
type Circuit struct {}
 
// 检查线路是否正常
// 为简单化 只返回true
func (c *Circuit) Check() error {
    return nil
}
 
// 开关
// SubSystem 角色
type Switch struct {
    Status string
}
 
// 检查线路是否正常
// 为简单化 只返回true
func (s *Switch) Check() error {
    switch s.Status {
    case "ON":
        return nil
    case "OFF":
        return errors.New("switch is off")
    default:
        return errors.New("switch unknow status")
    }
}
 
// 值周 管理所有子系统
// facade角色
type OnDuty struct {
    EquipmentSubSystem Equipment
    CircuitSubSystem Circuit
    SwitchSubSystem Switch
}
 
func (od *OnDuty) Check() {
    if err := od.EquipmentSubSystem.Check(); err != nil {
        fmt.Printf("OnDuty| check EquipmentSubSystem failed[%v].\n", err)
 
        return
    }
 
    if err := od.CircuitSubSystem.Check(); err != nil {
        fmt.Printf("OnDuty| check CircuitSubSystem failed[%v].\n", err)
 
        return
    }
 
    if err := od.SwitchSubSystem.Check(); err != nil {
        fmt.Printf("OnDuty| check SwitchSubSystem failed[%v].\n", err)
 
        return
    }
 
    fmt.Printf("OnDuty| check all system success.\n")
 
    return
}
 
func main() {
    onDuty := OnDuty {
        SwitchSubSystem : Switch {
            Status : "OFF",
        },
    }
 
    onDuty.Check()
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值