关于go:Go设计模式之装饰器模式

3次阅读

共计 2645 个字符,预计需要花费 7 分钟才能阅读完成。

装璜器模式也被称为包装器模式,指的是在不扭转原有对象属性和办法的根底上,动静的给原有对象增加一些新的性能和属性。具体代码如下:

  • 先定义一个专用的 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

    上述后果能够看出,程序曾经实现了所有的需要。

正文完
 0