golang架構(gòu)設(shè)計(jì)開閉原則手寫實(shí)現(xiàn)
緣起
最近復(fù)習(xí)設(shè)計(jì)模式
拜讀譚勇德的<<設(shè)計(jì)模式就該這樣學(xué)>>
該書以java語言演繹了常見設(shè)計(jì)模式
本系列筆記擬采用golang練習(xí)之
開閉原則
- 開閉原則(Open-Closed Principle, OCP)指一個(gè)軟件實(shí)體如類、模塊和函數(shù)應(yīng)該對擴(kuò)展開放,對修改關(guān)閉。所謂開閉,也正是對擴(kuò)展和修改兩個(gè)行為的一個(gè)原則。
- 實(shí)現(xiàn)開閉原則的核心思想就是面向抽象編程。
場景
- 某線上學(xué)習(xí)平臺, 提供系列課程產(chǎn)品(接口: ICourse)
- 每個(gè)課程有id,name,price等屬性
- 現(xiàn)在平臺搞促銷, golang課程(GolangCourse)打六折
- 如何上架打折課程? 是直接修改原golang課程的價(jià)格, 還是增加折后golang課程?
思路
- 開閉原則, 就是盡量避免修改, 改以擴(kuò)展的方式, 實(shí)現(xiàn)系統(tǒng)功能的增加
- 增加"優(yōu)惠折扣"接口 - IDiscount
- 增加"折后golang課程" - DiscountedGolangCourse, 同時(shí)實(shí)現(xiàn)課程接口和折扣接口
- DiscountedGolangCourse繼承自GolangCourse, 添加實(shí)現(xiàn)折扣接口, 并覆蓋ICourse.price()方法
ICourse.go
principles/open_close/ICourse.go
課程接口
package open_close type ICourse interface { ID() int Name() string Price() float64 }
GolangCourse.go
principles/open_close/GolangCourse.go
golang課程類, 實(shí)現(xiàn)ICourse接口
package open_close type GolangCourse struct { iID int sName string fPrice float64 } func NewGolangCourse(id int, name string, price float64) ICourse { return &GolangCourse{ iID: id, sName: name, fPrice: price, } } func (me *GolangCourse) ID() int { return me.iID } func (me *GolangCourse) Name() string { return me.sName } func (me *GolangCourse) Price() float64 { return me.fPrice }
IDiscount.go
principles/open_close/IDiscount.go
折扣接口
package open_close type IDiscount interface { Discount() float64 }
DiscountedGolangCourse.go
principles/open_close/DiscountedGolangCourse.go
該課程同時(shí)實(shí)現(xiàn)ICourse和IDiscount接口
package open_close type DiscountedGolangCourse struct { GolangCourse fDiscount float64 } func NewDiscountedGolangCourse(id int, name string, price float64, discount float64) ICourse { return &DiscountedGolangCourse{ GolangCourse: GolangCourse{ iID: id, sName: name, fPrice: price, }, fDiscount : discount, } } // implements IDiscount.Discount func (me *DiscountedGolangCourse) Discount() float64 { return me.fDiscount } // overwrite ICourse.Price func (me *DiscountedGolangCourse) Price() float64 { return me.fDiscount * me.GolangCourse.Price() }
open_close_test.go
main/open_close_test.go
課程接口測試用例
package main import ( "testing" ) import (ocp "learning/gooop/principles/open_close") func Test_open_close(t *testing.T) { fnShowCourse := func(it ocp.ICourse) { t.Logf("id=%v, name=%v, price=%v\n", it.ID(), it.Name(), it.Price()) } c1 := ocp.NewGolangCourse(1, "golang課程", 100) fnShowCourse(c1) c2 := ocp.NewDiscountedGolangCourse(2, "golang優(yōu)惠課程", 100, 0.6) fnShowCourse(c2) }
測試
$> go test -v main/open_close_test.go === RUN Test_open_close open_close_test.go:10: id=1, name=golang課程, price=100 open_close_test.go:10: id=2, name=golang優(yōu)惠課程, price=60 --- PASS: Test_open_close (0.00s) PASS ok command-line-arguments 0.001s
以上就是golang架構(gòu)設(shè)計(jì)開閉原則手寫實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于golang架構(gòu)設(shè)計(jì)開閉原則的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Go語言中new和make關(guān)鍵字的區(qū)別
本篇文章來介紹一道非常常見的面試題,到底有多常見呢?可能很多面試的開場白就是由此開始的。那就是 new 和 make 這兩個(gè)內(nèi)置函數(shù)的區(qū)別,希望對大家有所幫助2023-03-03基于Golang實(shí)現(xiàn)Excel表格的導(dǎo)入導(dǎo)出功能
最近項(xiàng)目開發(fā)中有涉及到Excel的導(dǎo)入與導(dǎo)出功能,特別是導(dǎo)出表格時(shí)需要特定的格式,所以本文給大家介紹了基于Golang實(shí)現(xiàn)Excel表格的導(dǎo)入導(dǎo)出功能,文中通過代碼示例和圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12一篇文章讀懂Golang?init函數(shù)執(zhí)行順序
init()函數(shù)會在包被初始化后自動(dòng)執(zhí)行,并且在main()函數(shù)之前執(zhí)行,但是需要注意的是init()以及main()函數(shù)都是無法被顯式調(diào)用的,下面這篇文章主要給大家介紹了關(guān)于如何通過一篇文章讀懂Golang?init函數(shù)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下2022-11-11Go 如何基于IP限制HTTP訪問頻率的方法實(shí)現(xiàn)
這篇文章主要介紹了Go 如何基于IP限制HTTP訪問頻率的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11GoLang實(shí)現(xiàn)Viper庫的封裝流程詳解
Viper是一個(gè)用于Go語言應(yīng)用程序的配置管理庫,它提供了一種簡單而靈活的方式來處理應(yīng)用程序的配置,支持多種格式的配置文件,這篇文章主要介紹了GoLang封裝Viper庫的流程,感興趣的同學(xué)可以參考下文2023-05-05一文帶你深入了解Golang中的參數(shù)傳遞機(jī)制
值傳遞和引用傳遞是編程語言中兩種主要的參數(shù)傳遞方式,決定了函數(shù)調(diào)用過程中實(shí)參如何影響形參以及函數(shù)內(nèi)部對形參的修改是否會影響到原始實(shí)參,下面就跟隨小編一起深入了解下golang中參數(shù)傳遞機(jī)制吧2024-01-01