目录

装饰器模式

装饰器模式也被称为包装器模式,指的是在不改变原有对象属性和方法的基础上,动态的给原有对象添加一些新的功能和属性。

具体案例代码如下:

基础手机的需求

先定义一个公用的interface Phone,提供两个方法, 一个是设置手机的颜色,一个是获取手机的价格:

type Phone interface {
  SelectColor(color string) string
  GetPrice() int
}

定义一个基础版的手机对象,该对象有尺寸、颜色、价格、内存、像素基础字段,该对象实现Phone接口。

type BasePhone struct {
  Size   int
  Price  int
  Color  string
  Memory int
  Pixel  int
}
func (p *BasePhone) SelectColor(color string) string {
  p.Color = color
  return color
}
func (p *BasePhone) GetPrice() int {
  return p.Price
}

上一步已经实现基础手机的需求,但是手机售卖会有不同的系列,拍照手机广受年轻人的喜爱,所以需要推出一个拍照手机系列,拍照手机具备基础版手机的功能,需要在此基础上新增手机像素的方法,调整基础像素,实现价格和颜色方法,那么就需要组合BasePhone。

type CameraPhone struct {
  BasePhone BasePhone
}
func (c *CameraPhone) SelectColor(color string) string {
  return c.BasePhone.SelectColor(color)
}
func (c *CameraPhone) GetPrice() int {
  return c.BasePhone.GetPrice() + CM
}
func (c *CameraPhone) GetPixel() int {
  c.BasePhone.Pixel += 50000000
  return c.BasePhone.Pixel
}

同样的,除了拍照手机系列,还有一个plus版本,这个版本比基础版本多了128G内存,所以需要单独实现一个调整内存的方法。

调整内存的方法

type PhonePlus struct {
  BasePhone BasePhone
}
func (p *PhonePlus) SelectColor(color string) string {
  p.BasePhone.SelectColor(color)
  return p.BasePhone.Color
}
func (p *PhonePlus) GetPrice() int {
  return p.BasePhone.GetPrice() + MP
}
func (p *PhonePlus) GetMemory() int {
  return p.BasePhone.Memory + 128*1024
}

接着推出一款plusplus加强版,这个版本像素和拍照版本一致,都是1亿像素,内存比plus版本又多了128G,这个版本以plus版为基础实现,需要实现设置颜色、获取价格、获取像素、获取内存的方法。

type PhonePlusPlus struct {
  PhonePlus PhonePlus
}
func (p *PhonePlusPlus) SelectColor(color string) string {
  return p.PhonePlus.SelectColor(color)
}
func (p *PhonePlusPlus) GetPrice() int {
  return p.PhonePlus.GetPrice() + MP
}
func (p *PhonePlusPlus) GetPixel() int {
  return p.PhonePlus.BasePhone.Pixel + 50000000
}
func (p *PhonePlusPlus) GetMemory() int {
  return p.PhonePlus.GetMemory() + 128*1024
}

运行程序

最后,运行下程序看看结果

package main
import "fmt"
func main() {
  basePhone := BasePhone{
      Size:   1000,
      Color:  "red",
      Price:  1000,
      Memory: 128 * 1024,
      Pixel:  50000000,
  }
  fmt.Printf("基础版的价格: %d, 颜色: %s, 像素为:%d, 内存为: %d\n", basePhone.GetPrice(), basePhone.SelectColor("纯黑"), basePhone.Pixel, basePhone.Memory)
  camaraPhone := CameraPhone{
      BasePhone: basePhone,
  }
  fmt.Printf("拍照版的价格: %d, 颜色: %s, 像素为:%d, 内存为: %d\n", camaraPhone.GetPrice(), camaraPhone.SelectColor("宝石蓝"), camaraPhone.GetPixel(), camaraPhone.BasePhone.Memory)
  phonePlus := PhonePlus{
      BasePhone: basePhone,
  }
  fmt.Printf("plus版的价格: %d, 颜色: %s, 像素为:%d, 内存为: %d\n", phonePlus.GetPrice(), phonePlus.SelectColor("玫瑰金"), phonePlus.BasePhone.Pixel, phonePlus.GetMemory())
  phonePlusPlus := PhonePlusPlus{
      PhonePlus: phonePlus,
  }
  fmt.Printf("plus Plus版的价格: %d, 颜色: %s, 像素为:%d, 内存为: %d\n", phonePlusPlus.GetPrice(), phonePlusPlus.SelectColor("青山黛"), phonePlusPlus.GetPixel(), phonePlusPlus.GetMemory())
}

结果:

基础版的价格: 1000, 颜色: 纯黑, 像素为:50000000, 内存为: 131072
拍照版的价格: 1600, 颜色: 宝石蓝, 像素为:100000000, 内存为: 131072     
plus版的价格: 1500, 颜色: 玫瑰金, 像素为:50000000, 内存为: 262144      
plus Plus版的价格: 2000, 颜色: 青山黛, 像素为:100000000, 内存为: 393216

上述结果可以看出,程序已经实现了所有的需求。

您可能感兴趣的文章: