最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang架構(gòu)設(shè)計(jì)開(kāi)閉原則手寫(xiě)實(shí)現(xiàn)

 更新時(shí)間:2022年07月12日 11:05:13   作者:ioly  
這篇文章主要為大家介紹了golang架構(gòu)設(shè)計(jì)開(kāi)閉原則手寫(xiě)實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

緣起

最近復(fù)習(xí)設(shè)計(jì)模式

拜讀譚勇德的<<設(shè)計(jì)模式就該這樣學(xué)>>

該書(shū)以java語(yǔ)言演繹了常見(jiàn)設(shè)計(jì)模式

本系列筆記擬采用golang練習(xí)之

開(kāi)閉原則

  • 開(kāi)閉原則(Open-Closed Principle, OCP)指一個(gè)軟件實(shí)體如類、模塊和函數(shù)應(yīng)該對(duì)擴(kuò)展開(kāi)放,對(duì)修改關(guān)閉。所謂開(kāi)閉,也正是對(duì)擴(kuò)展和修改兩個(gè)行為的一個(gè)原則。
  • 實(shí)現(xiàn)開(kāi)閉原則的核心思想就是面向抽象編程。

場(chǎng)景

  • 某線上學(xué)習(xí)平臺(tái), 提供系列課程產(chǎn)品(接口: ICourse)
  • 每個(gè)課程有id,name,price等屬性
  • 現(xiàn)在平臺(tái)搞促銷(xiāo), golang課程(GolangCourse)打六折
  • 如何上架打折課程? 是直接修改原golang課程的價(jià)格, 還是增加折后golang課程?

思路

  • 開(kāi)閉原則, 就是盡量避免修改, 改以擴(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ì)開(kāi)閉原則手寫(xiě)實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于golang架構(gòu)設(shè)計(jì)開(kāi)閉原則的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Golang實(shí)現(xiàn)SSH、SFTP操作小結(jié)

    Golang實(shí)現(xiàn)SSH、SFTP操作小結(jié)

    在日常的一些開(kāi)發(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ù)方法

    這篇文章主要介紹了golang數(shù)組-----尋找數(shù)組中缺失的整數(shù)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Go中的錯(cuò)誤和異常處理最佳實(shí)踐方法

    Go中的錯(cuò)誤和異常處理最佳實(shí)踐方法

    這篇文章主要介紹了Go中的錯(cuò)誤和異常處理最佳實(shí)踐方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容戒殺,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • 搭建Go語(yǔ)言的ORM框架Gorm的具體步驟(從Java到go)

    搭建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語(yǔ)言中g(shù)o?mod?vendor使用方法

    go mod vendor的功能是將新增的依賴包自動(dòng)寫(xiě)入當(dāng)前項(xiàng)目的 vendor目錄,下面這篇文章主要給大家介紹了關(guān)于Go語(yǔ)言中g(shù)o?mod?vendor使用的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Go高效率開(kāi)發(fā)Web參數(shù)校驗(yàn)三種方式實(shí)例

    Go高效率開(kāi)發(fā)Web參數(shù)校驗(yàn)三種方式實(shí)例

    這篇文章主要介紹了Go高效率開(kāi)發(fā)Web參數(shù)校驗(yàn)三種方式實(shí)例,需要的朋友可以參考下
    2022-11-11
  • Go數(shù)據(jù)結(jié)構(gòu)之HeapMap實(shí)現(xiàn)指定Key刪除堆

    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)求的幾種常見(jiàn)情況

    詳解golang中發(fā)送http請(qǐng)求的幾種常見(jiàn)情況

    這篇文章主要介紹了詳解golang中發(fā)送http請(qǐng)求的幾種常見(jiàn)情況,文中通過(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)

    這篇文章主要介紹了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
  • Golang的關(guān)鍵字defer的使用方法

    Golang的關(guān)鍵字defer的使用方法

    這篇文章主要介紹了Golang的關(guān)鍵字defer的使用方法,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-06-06

最新評(píng)論

高邮市| 冀州市| 广德县| 安义县| 新晃| 龙游县| 崇左市| 道孚县| 高碑店市| 木兰县| 体育| 顺平县| 斗六市| 屯门区| 玛纳斯县| 惠东县| 秭归县| 新民市| 错那县| 云阳县| 汉川市| 赤城县| 武陟县| 武安市| 通山县| 满城县| 江永县| 伊金霍洛旗| 淳化县| 衡阳市| 根河市| 鄄城县| 延长县| 健康| 响水县| 弥渡县| 永修县| 封开县| 龙里县| 建德市| 理塘县|