mirror of
https://github.com/tmrts/go-patterns.git
synced 2026-02-04 06:46:18 +00:00
Modify strategy pattern
This commit is contained in:
25
strategy/strategy.go
Normal file
25
strategy/strategy.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package strategy
|
||||
|
||||
type Operator interface {
|
||||
Apply(int, int) int
|
||||
}
|
||||
|
||||
type Operation struct {
|
||||
Operator Operator
|
||||
}
|
||||
|
||||
func (o *Operation) Operate(leftValue, rightValue int) int {
|
||||
return o.Operator.Apply(leftValue, rightValue)
|
||||
}
|
||||
|
||||
type Multiplication struct{}
|
||||
|
||||
func (Multiplication) Apply(lval, rval int) int {
|
||||
return lval * rval
|
||||
}
|
||||
|
||||
type Addition struct{}
|
||||
|
||||
func (Addition) Apply(lval, rval int) int {
|
||||
return lval + rval
|
||||
}
|
||||
Reference in New Issue
Block a user