通過(guò)手機(jī)案例理解Go設(shè)計(jì)模式之裝飾器模式的功能屬性
裝飾器模式
裝飾器模式也被稱(chēng)為包裝器模式,指的是在不改變?cè)袑?duì)象屬性和方法的基礎(chǔ)上,動(dòng)態(tài)的給原有對(duì)象添加一些新的功能和屬性。
具體案例代碼如下:
基礎(chǔ)手機(jī)的需求
先定義一個(gè)公用的interface Phone,提供兩個(gè)方法, 一個(gè)是設(shè)置手機(jī)的顏色,一個(gè)是獲取手機(jī)的價(jià)格:
type Phone interface {
SelectColor(color string) string
GetPrice() int
}定義一個(gè)基礎(chǔ)版的手機(jī)對(duì)象,該對(duì)象有尺寸、顏色、價(jià)格、內(nèi)存、像素基礎(chǔ)字段,該對(duì)象實(shí)現(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)實(shí)現(xiàn)基礎(chǔ)手機(jī)的需求,但是手機(jī)售賣(mài)會(huì)有不同的系列,拍照手機(jī)廣受年輕人的喜愛(ài),所以需要推出一個(gè)拍照手機(jī)系列,拍照手機(jī)具備基礎(chǔ)版手機(jī)的功能,需要在此基礎(chǔ)上新增手機(jī)像素的方法,調(diào)整基礎(chǔ)像素,實(shí)現(xiàn)價(jià)格和顏色方法,那么就需要組合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
}同樣的,除了拍照手機(jī)系列,還有一個(gè)plus版本,這個(gè)版本比基礎(chǔ)版本多了128G內(nèi)存,所以需要單獨(dú)實(shí)現(xiàn)一個(gè)調(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加強(qiáng)版,這個(gè)版本像素和拍照版本一致,都是1億像素,內(nèi)存比plus版本又多了128G,這個(gè)版本以plus版為基礎(chǔ)實(shí)現(xiàn),需要實(shí)現(xiàn)設(shè)置顏色、獲取價(jià)格、獲取像素、獲取內(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
}運(yùn)行程序
最后,運(yùn)行下程序看看結(jié)果
package main
import "fmt"
func main() {
basePhone := BasePhone{
Size: 1000,
Color: "red",
Price: 1000,
Memory: 128 * 1024,
Pixel: 50000000,
}
fmt.Printf("基礎(chǔ)版的價(jià)格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", basePhone.GetPrice(), basePhone.SelectColor("純黑"), basePhone.Pixel, basePhone.Memory)
camaraPhone := CameraPhone{
BasePhone: basePhone,
}
fmt.Printf("拍照版的價(jià)格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", camaraPhone.GetPrice(), camaraPhone.SelectColor("寶石藍(lán)"), camaraPhone.GetPixel(), camaraPhone.BasePhone.Memory)
phonePlus := PhonePlus{
BasePhone: basePhone,
}
fmt.Printf("plus版的價(jià)格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", phonePlus.GetPrice(), phonePlus.SelectColor("玫瑰金"), phonePlus.BasePhone.Pixel, phonePlus.GetMemory())
phonePlusPlus := PhonePlusPlus{
PhonePlus: phonePlus,
}
fmt.Printf("plus Plus版的價(jià)格: %d, 顏色: %s, 像素為:%d, 內(nèi)存為: %d\n", phonePlusPlus.GetPrice(), phonePlusPlus.SelectColor("青山黛"), phonePlusPlus.GetPixel(), phonePlusPlus.GetMemory())
}結(jié)果:
基礎(chǔ)版的價(jià)格: 1000, 顏色: 純黑, 像素為:50000000, 內(nèi)存為: 131072
拍照版的價(jià)格: 1600, 顏色: 寶石藍(lán), 像素為:100000000, 內(nèi)存為: 131072
plus版的價(jià)格: 1500, 顏色: 玫瑰金, 像素為:50000000, 內(nèi)存為: 262144
plus Plus版的價(jià)格: 2000, 顏色: 青山黛, 像素為:100000000, 內(nèi)存為: 393216
上述結(jié)果可以看出,程序已經(jīng)實(shí)現(xiàn)了所有的需求。
以上就是Go設(shè)計(jì)模式之裝飾器模式的詳細(xì)內(nèi)容,更多關(guān)于Go設(shè)計(jì)模式之裝飾器模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Go fmt包中Scan獲取標(biāo)準(zhǔn)輸入方式
Go的fmt.Scan、Scanf、Scanln用于輸入處理,Scan持續(xù)讀取至換行,Scanln按行讀取,Scanf按格式解析參數(shù),格式字符串中的空格和逗號(hào)影響參數(shù)分割,且輸入格式需與指定一致2025-07-07
go 代碼格式化和風(fēng)格開(kāi)發(fā)者指南
這篇文章主要為大家介紹了go 代碼格式化和風(fēng)格開(kāi)發(fā)者指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
淺談Go語(yǔ)言不提供隱式數(shù)字轉(zhuǎn)換的原因
本文主要介紹了淺談Go語(yǔ)言不提供隱式數(shù)字轉(zhuǎn)換的原因,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
golang中channel+error來(lái)做異步錯(cuò)誤處理有多香
官方推薦golang中錯(cuò)誤處理當(dāng)做值處理, 既然是值那就可以在channel中傳輸,這篇文章主要介紹了golang 錯(cuò)誤處理channel+error真的香,需要的朋友可以參考下2023-01-01
go語(yǔ)言通過(guò)反射獲取和設(shè)置結(jié)構(gòu)體字段值的方法
這篇文章主要介紹了go語(yǔ)言通過(guò)反射獲取和設(shè)置結(jié)構(gòu)體字段值的方法,實(shí)例分析了Go語(yǔ)言反射的使用技巧,需要的朋友可以參考下2015-03-03

