缘起
最近复习设计模式
拜读谭勇德的<<设计模式就该这样学>>
该书以java语言演绎了常见设计模式
本系列笔记拟采用golang练习之
开闭原则
- 开闭原则(Open-Closed Principle, OCP)指一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。所谓开闭,也正是对扩展和修改两个行为的一个原则。
- 实现开闭原则的核心思想就是面向抽象编程。
场景
- 某线上学习平台, 提供系列课程产品(接口: ICourse)
- 每个课程有id,name,price等属性
- 现在平台搞促销, golang课程(GolangCourse)打六折
- 如何上架打折课程? 是直接修改原golang课程的价格, 还是增加折后golang课程?
思路
- 开闭原则, 就是尽量避免修改, 改以扩展的方式, 实现系统功能的增加
- 增加”优惠折扣”接口 - IDiscount
- 增加”折后golang课程” - DiscountedGolangCourse, 同时实现课程接口和折扣接口
- 课程接口继承自原GolangCourse接口, 添加实现折扣接口, 并覆盖ICourse.price()方法
ICourse.go
principles/open_close/ICourse.go
课程接口
package open_closetype ICourse interface {ID() intName() stringPrice() float64}
GolangCourse.go
principles/open_close/GolangCourse.go
golang课程类, 实现ICourse接口
package open_closetype GolangCourse struct {iID intsName stringfPrice 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_closetype IDiscount interface {Discount() float64}
DiscountedGolangCourse.go
principles/open_close/DiscountedGolangCourse.go
该课程同时实现ICourse和IDiscount接口
package open_closetype DiscountedGolangCourse struct {GolangCoursefDiscount 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.Discountfunc (me *DiscountedGolangCourse) Discount() float64 {return me.fDiscount}// overwrite ICourse.Pricefunc (me *DiscountedGolangCourse) Price() float64 {return me.fDiscount * me.GolangCourse.Price()}
open_close_test.go
main/open_close_test.go
课程接口测试用例
package mainimport ("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优惠课程", 100, 0.6)fnShowCourse(c2)}
测试
$> go test -v main/open_close_test.go=== RUN Test_open_closeopen_close_test.go:10: id=1, name=golang课程, price=100open_close_test.go:10: id=2, name=golang优惠课程, price=60--- PASS: Test_open_close (0.00s)PASSok command-line-arguments 0.001s
