mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-23 04:16:02 +03:00
27 lines
475 B
Go
27 lines
475 B
Go
|
package factorymethod
|
||
|
|
||
|
import "testing"
|
||
|
|
||
|
func compute(factory OperatorFactory, a, b int) int {
|
||
|
op := factory.Create()
|
||
|
op.SetA(a)
|
||
|
op.SetB(b)
|
||
|
return op.Result()
|
||
|
}
|
||
|
|
||
|
func TestOperator(t *testing.T) {
|
||
|
var (
|
||
|
factory OperatorFactory
|
||
|
)
|
||
|
|
||
|
factory = PlusOperatorFactory{}
|
||
|
if compute(factory, 1, 2) != 3 {
|
||
|
t.Fatal("error with factory method pattern")
|
||
|
}
|
||
|
|
||
|
factory = MinusOperatorFactory{}
|
||
|
if compute(factory, 4, 2) != 2 {
|
||
|
t.Fatal("error with factory method pattern")
|
||
|
}
|
||
|
}
|