通過手機案例理解Go設計模式之裝飾器模式的功能屬性
裝飾器模式
裝飾器模式也被稱為包裝器模式,指的是在不改變原有對象屬性和方法的基礎上,動態(tài)的給原有對象添加一些新的功能和屬性。
具體案例代碼如下:
基礎手機的需求
先定義一個公用的interface Phone,提供兩個方法, 一個是設置手機的顏色,一個是獲取手機的價格:
type Phone interface { SelectColor(color string) string GetPrice() int }
定義一個基礎版的手機對象,該對象有尺寸、顏色、價格、內(nèi)存、像素基礎字段,該對象實現(xiàn)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 }
上一步已經(jīng)實現(xiàn)基礎手機的需求,但是手機售賣會有不同的系列,拍照手機廣受年輕人的喜愛,所以需要推出一個拍照手機系列,拍照手機具備基礎版手機的功能,需要在此基礎上新增手機像素的方法,調(diào)整基礎像素,實現(xiàn)價格和顏色方法,那么就需要組合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內(nèi)存,所以需要單獨實現(xiàn)一個調(diào)整內(nèi)存的方法。
調(diào)整內(nèi)存的方法
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億像素,內(nèi)存比plus版本又多了128G,這個版本以plus版為基礎實現(xiàn),需要實現(xiàn)設置顏色、獲取價格、獲取像素、獲取內(nèi)存的方法。
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, 內(nèi)存為: %d\n", basePhone.GetPrice(), basePhone.SelectColor("純黑"), basePhone.Pixel, basePhone.Memory) camaraPhone := CameraPhone{ BasePhone: basePhone, } fmt.Printf("拍照版的價格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", camaraPhone.GetPrice(), camaraPhone.SelectColor("寶石藍"), camaraPhone.GetPixel(), camaraPhone.BasePhone.Memory) phonePlus := PhonePlus{ BasePhone: basePhone, } fmt.Printf("plus版的價格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", phonePlus.GetPrice(), phonePlus.SelectColor("玫瑰金"), phonePlus.BasePhone.Pixel, phonePlus.GetMemory()) phonePlusPlus := PhonePlusPlus{ PhonePlus: phonePlus, } fmt.Printf("plus Plus版的價格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", phonePlusPlus.GetPrice(), phonePlusPlus.SelectColor("青山黛"), phonePlusPlus.GetPixel(), phonePlusPlus.GetMemory()) }
結果:
基礎版的價格: 1000, 顏色: 純黑, 像素為:50000000, 內(nèi)存為: 131072
拍照版的價格: 1600, 顏色: 寶石藍, 像素為:100000000, 內(nèi)存為: 131072
plus版的價格: 1500, 顏色: 玫瑰金, 像素為:50000000, 內(nèi)存為: 262144
plus Plus版的價格: 2000, 顏色: 青山黛, 像素為:100000000, 內(nèi)存為: 393216
上述結果可以看出,程序已經(jīng)實現(xiàn)了所有的需求。
以上就是Go設計模式之裝飾器模式的詳細內(nèi)容,更多關于Go設計模式之裝飾器模式的資料請關注腳本之家其它相關文章!
相關文章
Go語言實現(xiàn)一個簡單的并發(fā)聊天室的項目實戰(zhàn)
本文主要介紹了Go語言實現(xiàn)一個簡單的并發(fā)聊天室的項目實戰(zhàn),文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03go如何優(yōu)雅關閉Graceful?Shutdown服務
這篇文章主要為大家介紹了go優(yōu)雅關閉Graceful?Shutdown服務詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05