Skip to content

Instantly share code, notes, and snippets.

@Marceloromeugoncalves
Created June 11, 2019 15:22
Show Gist options
  • Save Marceloromeugoncalves/39621817bae07ef09b475b36c9d69a43 to your computer and use it in GitHub Desktop.
Save Marceloromeugoncalves/39621817bae07ef09b475b36c9d69a43 to your computer and use it in GitHub Desktop.
Exemplo
void Main(string[] args)
{
Sorvete s = new Sorvete();
Console.WriteLine("Sorvete:");
Console.WriteLine("{0:c}", s.Preco);
Console.WriteLine();
SorveteComCobertura c;
c = new SorveteComCobertura(s);
Console.WriteLine("Sorvete com cobertura:");
Console.WriteLine("{0:c}", c.Preco);
Console.WriteLine();
SorveteComBalinha b;
b = new SorveteComBalinha(s);
Console.WriteLine("Sorvete com balinha:");
Console.WriteLine("{0:c}", b.Preco);
Console.WriteLine();
SorveteComCobertura cb;
cb = new SorveteComCobertura(b);
Console.WriteLine("Sorvete com cobertura E balinha:");
Console.WriteLine("{0:c}", cb.Preco);
}
//IComponent
interface ISorvete
{
//State
double Preco { get; }
}
//Component
sealed class Sorvete : ISorvete
{
//State
public double Preco
{
get
{
return 1.50;
}
}
}
//Decorator
class SorveteComCobertura : ISorvete
{
//Component : IComponent
ISorvete s;
public SorveteComCobertura(ISorvete s)
{
this.s = s;
}
//State
public double Preco
{
get
{
return this.s.Preco + 0.50;
}
}
}
//Decorator
class SorveteComBalinha : ISorvete
{
//Component : IComponent
ISorvete s;
public SorveteComBalinha(ISorvete s)
{
this.s = s;
}
//State
public double Preco
{
get
{
return this.s.Preco + 0.75;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment