Skip to content

Instantly share code, notes, and snippets.

@flaviorl-net
Created February 24, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaviorl-net/afcb97535e79693e824c2145640dad52 to your computer and use it in GitHub Desktop.
Save flaviorl-net/afcb97535e79693e824c2145640dad52 to your computer and use it in GitHub Desktop.
public class CalculadoraStrategy
{
public enum Operacao
{
None,
Sum,
Sub,
Mult,
Div
}
private readonly ContextStrategy<int, StrcNumeros, Operacao> _contextStrategy = new ContextStrategy<int, StrcNumeros, Operacao>();
public CalculadoraStrategy(string operacao)
{
Operacao op = Operacao.None;
try
{
op = (Operacao)Enum.Parse(typeof(Operacao), operacao, true);
}
catch (ArgumentException)
{
Console.WriteLine("Operação inválida!");
return;
}
_contextStrategy.AddList(Operacao.Sum, new Sum());
_contextStrategy.AddList(Operacao.Sub, new Sub());
_contextStrategy.AddList(Operacao.Mult, new Mult());
_contextStrategy.AddList(Operacao.Div, new Div());
SelecionarCalculo(op);
}
private void SelecionarCalculo(Operacao operacao)
{
_contextStrategy.SetStrategy(operacao);
}
public int Calcular(int numero1, int numero2)
{
return _contextStrategy.Execute(new StrcNumeros() { Numero1 = numero1, Numero2 = numero2 });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment