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
cdeee5d
commit 38ee850
Showing
7 changed files
with
104 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,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(); | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
using System; | ||
|
||
namespace Observer | ||
{ | ||
public class AssuntoConcreto : Assunto | ||
{ | ||
public string EstadoAssunto { get; set; } | ||
} | ||
} |
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,9 @@ | ||
using System; | ||
|
||
namespace Observer | ||
{ | ||
public abstract class Observador | ||
{ | ||
public abstract void Update(); | ||
} | ||
} |
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,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; } | ||
} | ||
} |
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 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(); | ||
} | ||
} | ||
} |
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