forked from LucasCancio/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ed8a29
commit 8445050
Showing
9 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
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
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()); | ||
} | ||
} | ||
} |
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
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; | ||
} | ||
} | ||
} |
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
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> |
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
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()); | ||
} | ||
} | ||
} |
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
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("--------------"); | ||
} | ||
} | ||
} | ||
} |
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
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); | ||
} | ||
} |
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
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()); | ||
} | ||
} | ||
} |
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
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()); | ||
} | ||
} | ||
} |
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