mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-29 08:26:10 +03:00
Modify strategy pattern
This commit is contained in:
parent
b10a4f9cac
commit
b1ddd2651b
@ -1,6 +1,4 @@
|
|||||||
package main
|
package strategy
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type Operator interface {
|
type Operator interface {
|
||||||
Apply(int, int) int
|
Apply(int, int) int
|
||||||
@ -25,15 +23,3 @@ type Addition struct{}
|
|||||||
func (Addition) Apply(lval, rval int) int {
|
func (Addition) Apply(lval, rval int) int {
|
||||||
return lval + rval
|
return lval + rval
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
|
||||||
mult := Operation{Multiplication{}}
|
|
||||||
|
|
||||||
// Outputs 15
|
|
||||||
fmt.Println(mult.Operate(3, 5))
|
|
||||||
|
|
||||||
pow := Operation{Addition{}}
|
|
||||||
|
|
||||||
// Outputs 8
|
|
||||||
fmt.Println(pow.Operate(3, 5))
|
|
||||||
}
|
|
18
strategy/strategy_test.go
Normal file
18
strategy/strategy_test.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package strategy
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestMultiplicationStrategy(t *testing.T) {
|
||||||
|
mult := Operation{Multiplication{}}
|
||||||
|
|
||||||
|
if res := mult.Operate(3, 5); res != 15 {
|
||||||
|
t.Errorf("Multiplication.Operate(3, 5) expected 15 got %q", res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAdditionStrategy(t *testing.T) {
|
||||||
|
add := Operation{Addition{}}
|
||||||
|
if res := add.Operate(3, 5); res != 8 {
|
||||||
|
t.Errorf("Addition.Operate(3, 5) expected 8 got %q", res)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user