Go常用设计模式(下)

简介: Go常用设计模式(下)

装饰器模式和代理模式,其中装饰器模式还是比较常用的。

1 装饰器模式

1.1 概念

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。

这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

四个重要角色:

  • 抽象组件Component:一个接口或者抽象类,是定义我们最核心的对象(最原始的对象),被装饰类的抽象。
  • 具体组件ConcreteComponent:最核心、最原始、最基本的接口或抽象类Component的实现,可以单独用,也可将其进行装饰,比如上面的简单肉夹馍。
  • 抽象装饰者Decorator:一般是一个抽象类,继承或实现Component,在它的属性里面有一个变量为Component抽象构件,是装饰器最关键的地方。
  • 具体装饰者ConcreteDecorator:可以把基础构件装饰成新的东西。
1.2 代码
type PlusPhone func(phone string) string
func PhoneDecorate(fn PlusPhone) PlusPhone {
   return func(phone string) string {
      phone = phone + " plus"
      return fn(phone)
   }
}

测试:

func TestDecorator(t *testing.T) {
   f := structural.PhoneDecorate(func(phone string) string {
      return phone
   })
   s := f("iphone X")
   fmt.Println(s)
}

2 代理模式

2.1 概念

代理简单的说就是接口代理实现类,当一个或多个类实现一个接口后,需要将接口的方法进行实现,当类进行实例化时使用接口进行代理方法的实现,直接调用接口的方法而不是实现类的方法。

2.2 代码
type Image interface {
   Display()
}
type RealImage struct {
   FileName string
}
func NewRealImage(file string) *RealImage {
   return &RealImage{
      FileName: file,
   }
}
func (img *RealImage) Display() {
   fmt.Printf("Displaying %s.\n", img.FileName)
}
type ProxyImage struct {
   Real     *RealImage
   FileName string
}
func NewProxyImage(file string) *ProxyImage {
   return &ProxyImage{
      FileName: file,
   }
}
func (img *ProxyImage) Display() {
   if img.Real == nil {
      img.Real = NewRealImage(img.FileName)
   }
   img.Real.Display()
}

测试:

func TestProxy(t *testing.T) {
   proxyImage := structural.NewProxyImage("zs")
   proxyImage.Display()
}

3 总结

装饰器模式很容易理解,他就是在我们想要调用方法的时候增加一些额外的操作,这种操作我们就成为装饰,就是让对象经过装饰增加需要的属性或功能。

但是Go实现的代理模式和Java实现的相比不太容易理解,所以还需要我们多花一些时间来理解。

相关文章
|
17天前
|
设计模式 Go 网络安全
[设计模式 Go实现] 结构型~代理模式
[设计模式 Go实现] 结构型~代理模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 结构型~享元模式
[设计模式 Go实现] 结构型~享元模式
|
17天前
|
设计模式 Go API
[设计模式 Go实现] 结构型~外观模式
[设计模式 Go实现] 结构型~外观模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 结构型~组合模式
[设计模式 Go实现] 结构型~组合模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 结构型~装饰模式
[设计模式 Go实现] 结构型~装饰模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 结构型~适配器模式
[设计模式 Go实现] 结构型~适配器模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 行为型~解释器模式
[设计模式 Go实现] 行为型~解释器模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 行为型~迭代器模式
[设计模式 Go实现] 行为型~迭代器模式
|
17天前
|
设计模式 存储 Go
[设计模式 Go实现] 行为型~备忘录模式
[设计模式 Go实现] 行为型~备忘录模式
|
17天前
|
设计模式 Go
[设计模式 Go实现] 行为型~中介者模式
[设计模式 Go实现] 行为型~中介者模式