golang架構(gòu)設(shè)計(jì)開閉原則手寫實(shí)現(xiàn)
緣起
最近復(fù)習(xí)設(shè)計(jì)模式
拜讀譚勇德的<<設(shè)計(jì)模式就該這樣學(xué)>>
該書以java語(yǔ)言演繹了常見設(shè)計(jì)模式
本系列筆記擬采用golang練習(xí)之
開閉原則
- 開閉原則(Open-Closed Principle, OCP)指一個(gè)軟件實(shí)體如類、模塊和函數(shù)應(yīng)該對(duì)擴(kuò)展開放,對(duì)修改關(guān)閉。所謂開閉,也正是對(duì)擴(kuò)展和修改兩個(gè)行為的一個(gè)原則。
- 實(shí)現(xiàn)開閉原則的核心思想就是面向抽象編程。
場(chǎng)景
- 某線上學(xué)習(xí)平臺(tái), 提供系列課程產(chǎn)品(接口: ICourse)
- 每個(gè)課程有id,name,price等屬性
- 現(xiàn)在平臺(tái)搞促銷, 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
課程接口測(cè)試用例
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)
}測(cè)試
$> 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ì)開閉原則的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang實(shí)現(xiàn)SSH、SFTP操作小結(jié)
在日常的一些開發(fā)場(chǎng)景中,我們需要去和遠(yuǎn)程服務(wù)器進(jìn)行一些通信,本文主要介紹了Golang實(shí)現(xiàn)SSH、SFTP操作小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
golang數(shù)組-----尋找數(shù)組中缺失的整數(shù)方法
這篇文章主要介紹了golang數(shù)組-----尋找數(shù)組中缺失的整數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
搭建Go語(yǔ)言的ORM框架Gorm的具體步驟(從Java到go)
很多朋友不知道如何使用Goland軟件,搭建一個(gè)ORM框架GORM,今天小編給大家分享一篇教程關(guān)于搭建Go語(yǔ)言的ORM框架Gorm的具體步驟(從Java到go),感興趣的朋友跟隨小編一起學(xué)習(xí)下吧2022-09-09
Go語(yǔ)言中g(shù)o?mod?vendor使用方法
go mod vendor的功能是將新增的依賴包自動(dòng)寫入當(dāng)前項(xiàng)目的 vendor目錄,下面這篇文章主要給大家介紹了關(guān)于Go語(yǔ)言中g(shù)o?mod?vendor使用的相關(guān)資料,需要的朋友可以參考下2022-10-10
Go高效率開發(fā)Web參數(shù)校驗(yàn)三種方式實(shí)例
這篇文章主要介紹了Go高效率開發(fā)Web參數(shù)校驗(yàn)三種方式實(shí)例,需要的朋友可以參考下2022-11-11
Go數(shù)據(jù)結(jié)構(gòu)之HeapMap實(shí)現(xiàn)指定Key刪除堆
這篇文章主要給大家介紹了Go語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之HeapMap實(shí)現(xiàn)指定Key刪除堆,通過(guò)使用Go語(yǔ)言中的container/heap包,我們可以輕松地實(shí)現(xiàn)一個(gè)優(yōu)先級(jí)隊(duì)列,文中有詳細(xì)的代碼示例講解,需要的朋友可以參考下2023-07-07
詳解golang中發(fā)送http請(qǐng)求的幾種常見情況
這篇文章主要介紹了詳解golang中發(fā)送http請(qǐng)求的幾種常見情況,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Golang 發(fā)送http請(qǐng)求時(shí)設(shè)置header的實(shí)現(xiàn)
這篇文章主要介紹了Golang 發(fā)送http請(qǐng)求時(shí)設(shè)置header的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

