Skip to content

Commit

Permalink
Composite
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCancio committed Jan 13, 2021
1 parent dd44f69 commit 46c1012
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
19 changes: 19 additions & 0 deletions PatternsEstruturais/Composite/Componente.cs
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);
}
}
35 changes: 35 additions & 0 deletions PatternsEstruturais/Composite/Composite.cs
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);
}
}
}
8 changes: 8 additions & 0 deletions PatternsEstruturais/Composite/Composite.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>
26 changes: 26 additions & 0 deletions PatternsEstruturais/Composite/Folha.cs
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!");
}
}
}
27 changes: 27 additions & 0 deletions PatternsEstruturais/Composite/Program.cs
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);
}
}
}
6 changes: 6 additions & 0 deletions PatternsEstruturais/PatternsEstruturais.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adapter", "Adapter\Adapter.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge", "Bridge\Bridge.csproj", "{02D55463-CDED-468F-9139-94FA89E255FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Composite\Composite.csproj", "{45CCBC20-6C3E-4903-994D-149446B2F51A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{02D55463-CDED-468F-9139-94FA89E255FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{02D55463-CDED-468F-9139-94FA89E255FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{02D55463-CDED-468F-9139-94FA89E255FA}.Release|Any CPU.Build.0 = Release|Any CPU
{45CCBC20-6C3E-4903-994D-149446B2F51A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45CCBC20-6C3E-4903-994D-149446B2F51A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45CCBC20-6C3E-4903-994D-149446B2F51A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45CCBC20-6C3E-4903-994D-149446B2F51A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 46c1012

Please sign in to comment.