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
dd44f69
commit 46c1012
Showing
6 changed files
with
121 additions
and
0 deletions.
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,19 @@ | ||
using System; | ||
|
||
namespace Composite | ||
{ | ||
public abstract class Componente | ||
{ | ||
protected string nome { get; set; } | ||
public Componente(string nome) | ||
{ | ||
this.nome = nome; | ||
} | ||
|
||
public abstract void Adicionar(Componente c); | ||
|
||
public abstract void Remover(Componente c); | ||
|
||
public abstract void Mostrar(int profundidade); | ||
} | ||
} |
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,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Composite | ||
{ | ||
public class Composite : Componente | ||
{ | ||
private List<Componente> filhos = new List<Componente>(); | ||
public Composite(string nome) | ||
: base(nome) | ||
{ | ||
|
||
} | ||
|
||
public override void Adicionar(Componente c) | ||
{ | ||
this.filhos.Add(c); | ||
} | ||
|
||
public override void Mostrar(int profundidade) | ||
{ | ||
Console.WriteLine(new string('-', profundidade) + nome); | ||
|
||
foreach (Componente item in this.filhos) | ||
{ | ||
item.Mostrar(profundidade + 2); | ||
} | ||
} | ||
|
||
public override void Remover(Componente c) | ||
{ | ||
this.filhos.Remove(c); | ||
} | ||
} | ||
} |
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,26 @@ | ||
using System; | ||
|
||
namespace Composite | ||
{ | ||
public class Folha : Componente | ||
{ | ||
public Folha(string nome) : base(nome) | ||
{ | ||
} | ||
|
||
public override void Adicionar(Componente c) | ||
{ | ||
Console.WriteLine("Não é possivel adicionar a folha!"); | ||
} | ||
|
||
public override void Mostrar(int profundidade) | ||
{ | ||
Console.WriteLine(new string('-', profundidade) + nome); | ||
} | ||
|
||
public override void Remover(Componente c) | ||
{ | ||
Console.WriteLine("Não é possivel remover a folha!"); | ||
} | ||
} | ||
} |
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,27 @@ | ||
using System; | ||
|
||
namespace Composite | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Composite root = new Composite("ROOT"); | ||
root.Adicionar(new Folha("Folha A")); | ||
root.Adicionar(new Folha("Folha B")); | ||
|
||
Composite comp = new Composite("Composite X"); | ||
comp.Adicionar(new Folha("Folha XA")); | ||
comp.Adicionar(new Folha("Folha XB")); | ||
|
||
root.Adicionar(comp); | ||
root.Adicionar(new Folha("Folha C")); | ||
|
||
Folha folha = new Folha("Folha D"); | ||
root.Adicionar(folha); | ||
root.Remover(folha); | ||
|
||
root.Mostrar(1); | ||
} | ||
} | ||
} |
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