commit 4037ea75d07b6b17a85c21f2b475e6500de89964 Author: ZGGSONG Date: Tue Feb 21 12:02:28 2023 +0800 feat: add factory diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f32e31a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..14579fe --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## 说明 + +《大话设计模式》 代码记录 diff --git a/factory/cash_factory.go b/factory/cash_factory.go new file mode 100644 index 0000000..5c63fdc --- /dev/null +++ b/factory/cash_factory.go @@ -0,0 +1,15 @@ +package factory + +func CreateCashAccept(t int) Casher { + var cash Casher + switch t { + case 1: //正常 + cash = new(CashNormal) + case 2: //8折 + cash = &CashRebate{moneyRebate: 0.8} + case 3: //满300减50 + cash = &CashReturn{moneyCondition: 300, moneyReturn: 50} + default: + } + return cash +} diff --git a/factory/cash_normal.go b/factory/cash_normal.go new file mode 100644 index 0000000..a207045 --- /dev/null +++ b/factory/cash_normal.go @@ -0,0 +1,7 @@ +package factory + +type CashNormal struct{} + +func (c *CashNormal) AcceptCash(money float64) float64 { + return money +} diff --git a/factory/cash_rebate.go b/factory/cash_rebate.go new file mode 100644 index 0000000..12c4431 --- /dev/null +++ b/factory/cash_rebate.go @@ -0,0 +1,11 @@ +package factory + +// CashRebate +// @Description: 打折收费 +type CashRebate struct { + moneyRebate float64 +} + +func (c *CashRebate) AcceptCash(money float64) float64 { + return money * c.moneyRebate +} diff --git a/factory/cash_return.go b/factory/cash_return.go new file mode 100644 index 0000000..2fd39c4 --- /dev/null +++ b/factory/cash_return.go @@ -0,0 +1,18 @@ +package factory + +import "math" + +// CashReturn +// @Description: 返利收费 +type CashReturn struct { + moneyCondition float64 + moneyReturn float64 +} + +func (c *CashReturn) AcceptCash(money float64) float64 { + ret := money + if money >= c.moneyCondition { + ret = money - math.Floor(money/c.moneyCondition)*c.moneyReturn + } + return ret +} diff --git a/factory/cash_test.go b/factory/cash_test.go new file mode 100644 index 0000000..af87047 --- /dev/null +++ b/factory/cash_test.go @@ -0,0 +1,47 @@ +package factory + +import "testing" + +func TestCashNormal_AcceptCash(t *testing.T) { + accept := CreateCashAccept(1) + if accept == nil { + t.Error("testing failed") + } + money := 300.0 + ret := accept.AcceptCash(money) + t.Log("\t应付: ", money) + t.Log("\t实付: ", ret) +} + +func TestCashRebate_AcceptCash(t *testing.T) { + accept := CreateCashAccept(2) + if accept == nil { + t.Error("testing failed") + } + money := 300.0 + ret := accept.AcceptCash(money) + t.Log("\t应付: ", money) + t.Log("\t实付: ", ret) +} + +func TestCashReturn_AcceptCash(t *testing.T) { + accept := CreateCashAccept(3) + if accept == nil { + t.Error("testing failed") + } + money := 300.0 + ret := accept.AcceptCash(money) + t.Log("\t应付: ", money) + t.Log("\t实付: ", ret) +} + +func TestCash(t *testing.T) { + accept := CreateCashAccept(4) + if accept == nil { + t.Fatal("testing failed") + } + money := 300.0 + ret := accept.AcceptCash(money) + t.Log("\t应付: ", money) + t.Log("\t实付: ", ret) +} diff --git a/factory/casher.go b/factory/casher.go new file mode 100644 index 0000000..0875082 --- /dev/null +++ b/factory/casher.go @@ -0,0 +1,5 @@ +package factory + +type Casher interface { + AcceptCash(money float64) float64 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9b64f50 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module designpatterns + +go 1.19