Skip to content

Commit

Permalink
Flyweight
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCancio committed Jan 14, 2021
1 parent 8ed8a29 commit 8445050
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 1 deletion.
18 changes: 18 additions & 0 deletions PatternsEstruturais/Flyweight/Azul.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Flyweight
{
public class Azul : Tartaruga
{
public Azul()
{
this.condicao = "Tartaruga dentro do casco, ";
this.acao = "rodando no chão - ";
}
public override void Mostra(string cor)
{
this.Cor = cor;
Console.WriteLine(condicao + acao + cor.ToUpper());
}
}
}
44 changes: 44 additions & 0 deletions PatternsEstruturais/Flyweight/Fabrica_Flyweight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;

namespace Flyweight
{
public class Fabrica_Flyweight
{
private readonly Dictionary<string, Tartaruga> lista_de_tartarugas = new Dictionary<string, Tartaruga>();

public Tartaruga GetTartaruga(string cor)
{
Tartaruga t = null;

if (lista_de_tartarugas.ContainsKey(cor))
{
t = lista_de_tartarugas[cor];
}
else
{
switch (cor)
{
case "azul":
t = new Azul();
break;
case "verde":
t = new Verde();
break;
case "vermelha":
t = new Vermelha();
break;
case "laranja":
t = new Laranja();
break;
default:
break;
}

lista_de_tartarugas.Add(cor, t);
}

return t;
}
}
}
8 changes: 8 additions & 0 deletions PatternsEstruturais/Flyweight/Flyweight.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
18 changes: 18 additions & 0 deletions PatternsEstruturais/Flyweight/Laranja.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Flyweight
{
public class Laranja : Tartaruga
{
public Laranja()
{
this.condicao = "Tartaruga dentro do casco, ";
this.acao = "rodando no chão - ";
}
public override void Mostra(string cor)
{
this.Cor = cor;
Console.WriteLine(condicao + acao + cor.ToUpper());
}
}
}
31 changes: 31 additions & 0 deletions PatternsEstruturais/Flyweight/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;

namespace Flyweight
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---# DESIGN PATTERN FLYWEIGHT #---");

var fabrica = new Fabrica_Flyweight();

string cor = string.Empty;

Tartaruga tartaruga;
while (true)
{
Console.WriteLine();
Console.WriteLine("Qual tartaruga enviar para tela: ");

cor = Console.ReadLine();

tartaruga = fabrica.GetTartaruga(cor);
tartaruga.Mostra(cor);

Console.WriteLine();
Console.WriteLine("--------------");
}
}
}
}
15 changes: 15 additions & 0 deletions PatternsEstruturais/Flyweight/Tartaruga.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Flyweight
{
public abstract class Tartaruga
{
protected string condicao;
protected string acao;
public string Cor { get; set; }

public abstract void Mostra(string cor);
}
}
18 changes: 18 additions & 0 deletions PatternsEstruturais/Flyweight/Verde.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Flyweight
{
class Verde : Tartaruga
{
public Verde()
{
this.condicao = "Tartaruga dentro do casco, ";
this.acao = "rodando no chão - ";
}
public override void Mostra(string cor)
{
this.Cor = cor;
Console.WriteLine(condicao + acao + cor.ToUpper());
}
}
}
18 changes: 18 additions & 0 deletions PatternsEstruturais/Flyweight/Vermelha.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Flyweight
{
public class Vermelha : Tartaruga
{
public Vermelha()
{
this.condicao = "Tartaruga dentro do casco, ";
this.acao = "rodando no chão - ";
}
public override void Mostra(string cor)
{
this.Cor = cor;
Console.WriteLine(condicao + acao + cor.ToUpper());
}
}
}
8 changes: 7 additions & 1 deletion PatternsEstruturais/PatternsEstruturais.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Composite", "Composite\Comp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{A1691928-5028-4FF7-8BEC-CAC23E760596}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Facade", "Facade\Facade.csproj", "{A0CB329A-28F4-4BDA-A889-168579E32B1B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facade", "Facade\Facade.csproj", "{A0CB329A-28F4-4BDA-A889-168579E32B1B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Flyweight", "Flyweight\Flyweight.csproj", "{E7A40169-9887-4FB6-BD6B-58B3A063569E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -39,6 +41,10 @@ Global
{A0CB329A-28F4-4BDA-A889-168579E32B1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0CB329A-28F4-4BDA-A889-168579E32B1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0CB329A-28F4-4BDA-A889-168579E32B1B}.Release|Any CPU.Build.0 = Release|Any CPU
{E7A40169-9887-4FB6-BD6B-58B3A063569E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7A40169-9887-4FB6-BD6B-58B3A063569E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7A40169-9887-4FB6-BD6B-58B3A063569E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7A40169-9887-4FB6-BD6B-58B3A063569E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 8445050

Please sign in to comment.