Last active
July 3, 2021 15:58
-
-
Save lemissel/af1b3d8de5ad69f07857d4cff7a55a9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WARNING: This is a simple anotation. Don`t run this code | |
type Operador func(int, int) int | |
type Calculadora struct { | |
op Operador | |
} | |
func (c Calculadora) Calcular(a int, b int) int { | |
return c.op(a,b) | |
} | |
// New Calculadora | |
func NewCalculadora(op Operacao) Calculadora { | |
return Calculadora{op} | |
} | |
// Some implementations | |
var Soma Operador = Operador(func(a int, b int) int { | |
return a + b | |
}) | |
var Prod Operador = Operador(func(a int, b int,) int { | |
return a * b | |
}) | |
func OperadorStrategy(strategia string) Operador { | |
var op Operador | |
switch strategia { | |
case "multiplicacao": | |
op = Prod | |
case "soma": | |
op = Soma | |
default: | |
op= Soma | |
} | |
return op | |
} | |
// Using... | |
func main() { | |
estrategia := flag.String("estrategia", "soma", "define a estrategia") | |
flag.Parse() | |
op := OperadorStrategy(*estrategia) | |
calculadora := NewCalculator(op) | |
calculadora.Calcular(1, 2) | |
// result 3 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment