Skip to content

Commit

Permalink
Observer
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCancio committed Jan 15, 2021
1 parent cdeee5d commit 38ee850
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
29 changes: 29 additions & 0 deletions PatternsComportamentais/Observer/Assunto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Observer
{
public abstract class Assunto
{
private List<Observador> _observadores = new List<Observador>();

public void Anexar(Observador observador)
{
this._observadores.Add(observador);
}

public void Desanexar(Observador observador)
{
this._observadores.Remove(observador);
}

public void Notificar()
{
foreach (Observador item in _observadores)
{
item.Update();
}
}
}
}
9 changes: 9 additions & 0 deletions PatternsComportamentais/Observer/AssuntoConcreto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Observer
{
public class AssuntoConcreto : Assunto
{
public string EstadoAssunto { get; set; }
}
}
9 changes: 9 additions & 0 deletions PatternsComportamentais/Observer/Observador.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Observer
{
public abstract class Observador
{
public abstract void Update();
}
}
25 changes: 25 additions & 0 deletions PatternsComportamentais/Observer/ObservadorConcreto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;

namespace Observer
{
public class ObservadorConcreto : Observador
{
private string _nome;
private string _estadoObservador;
private AssuntoConcreto _assunto;

public ObservadorConcreto(AssuntoConcreto assunto, string nome)
{
_assunto = assunto;
_nome = nome;
}

public override void Update()
{
this._estadoObservador = this._assunto.EstadoAssunto;
Console.WriteLine($"Observador {this._nome}, seu novo estado é: {this._estadoObservador}");
}

public AssuntoConcreto Assunto { get; set; }
}
}
8 changes: 8 additions & 0 deletions PatternsComportamentais/Observer/Observer.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 PatternsComportamentais/Observer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Observer
{
class Program
{
static void Main(string[] args)
{
AssuntoConcreto s = new AssuntoConcreto();
s.Anexar(new ObservadorConcreto(s, "X"));
s.Anexar(new ObservadorConcreto(s, "Y"));
s.Anexar(new ObservadorConcreto(s, "Z"));

s.EstadoAssunto = "ABC";
s.Notificar();
}
}
}
6 changes: 6 additions & 0 deletions PatternsComportamentais/PatternsComportamentais.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "Mediator\Mediat
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memento", "Memento\Memento.csproj", "{189CDE42-E8FF-4D76-851B-321F75172835}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Observer", "Observer\Observer.csproj", "{88C5750D-1C94-49EA-9402-81EC6DB545AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -45,6 +47,10 @@ Global
{189CDE42-E8FF-4D76-851B-321F75172835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{189CDE42-E8FF-4D76-851B-321F75172835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{189CDE42-E8FF-4D76-851B-321F75172835}.Release|Any CPU.Build.0 = Release|Any CPU
{88C5750D-1C94-49EA-9402-81EC6DB545AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{88C5750D-1C94-49EA-9402-81EC6DB545AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88C5750D-1C94-49EA-9402-81EC6DB545AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88C5750D-1C94-49EA-9402-81EC6DB545AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 38ee850

Please sign in to comment.