Update Observer pattern
This commit is contained in:
@@ -18,6 +18,8 @@ namespace BehavioralPatterns
|
|||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
ObserverPatternExamples.Run();
|
||||||
|
Console.ReadKey();
|
||||||
//Chain of responsibillity
|
//Chain of responsibillity
|
||||||
//This is usefull when you have a request and you don't know who should process it
|
//This is usefull when you have a request and you don't know who should process it
|
||||||
ChainOfResponsibillityExamples.Run();
|
ChainOfResponsibillityExamples.Run();
|
||||||
@@ -40,10 +42,6 @@ namespace BehavioralPatterns
|
|||||||
Console.ReadKey();
|
Console.ReadKey();
|
||||||
|
|
||||||
MementoPatternExamples.Run();
|
MementoPatternExamples.Run();
|
||||||
|
|
||||||
|
|
||||||
ObserverPatternExamples.Run();
|
|
||||||
Console.ReadKey();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,21 +11,16 @@ namespace ObserverPattern.Twits
|
|||||||
{
|
{
|
||||||
TwitObservable observable = new TwitObservable();
|
TwitObservable observable = new TwitObservable();
|
||||||
|
|
||||||
TwitUser t100 = new TwitUser("t100");
|
TwitUser t100 = new TwitUser("t100", observable);
|
||||||
TwitUser r2d2 = new TwitUser("r2d2");
|
TwitUser r2d2 = new TwitUser("R2-D2", observable);
|
||||||
|
|
||||||
var t100Subscription = observable.Subscribe(t100);
|
t100.Twit("El chupacapra - BOOM BOOM");
|
||||||
var r2d2Subscription = observable.Subscribe(r2d2);
|
|
||||||
|
|
||||||
observable.AddTwit("El chupacapra");
|
r2d2.Twit("Vamos vamos mi amor");
|
||||||
|
|
||||||
t100Subscription.Dispose();
|
t100.Dispose();
|
||||||
|
|
||||||
observable.AddTwit("Vamos vamos mi amor");
|
|
||||||
|
|
||||||
observable.ItsGoingHomeTime();
|
observable.ItsGoingHomeTime();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/ObserverPattern/Twits/Twit.cs
Normal file
14
src/ObserverPattern/Twits/Twit.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ObserverPattern.Twits
|
||||||
|
{
|
||||||
|
public class Twit
|
||||||
|
{
|
||||||
|
public TwitUser Emiter { get; set; }
|
||||||
|
|
||||||
|
public string Message { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,14 +8,14 @@ namespace ObserverPattern.Twits
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Concrete observable
|
/// Concrete observable
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TwitObservable : IObservable<string>
|
public class TwitObservable : IObservable<Twit>
|
||||||
{
|
{
|
||||||
List<IObserver<string>> observers;
|
List<IObserver<Twit>> observers;
|
||||||
public TwitObservable()
|
public TwitObservable()
|
||||||
{
|
{
|
||||||
observers = new List<IObserver<string>>();
|
observers = new List<IObserver<Twit>>();
|
||||||
}
|
}
|
||||||
public IDisposable Subscribe(IObserver<string> observer)
|
public IDisposable Subscribe(IObserver<Twit> observer)
|
||||||
{
|
{
|
||||||
if (!observers.Contains(observer))
|
if (!observers.Contains(observer))
|
||||||
observers.Add(observer);
|
observers.Add(observer);
|
||||||
@@ -23,9 +23,9 @@ namespace ObserverPattern.Twits
|
|||||||
return new Unsubscriber(observers, observer);
|
return new Unsubscriber(observers, observer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddTwit(string twit)
|
public void AddTwit(Twit twit)
|
||||||
{
|
{
|
||||||
foreach (var observer in observers)
|
foreach (var observer in observers.Where(o => o != twit.Emiter))
|
||||||
{
|
{
|
||||||
observer.OnNext(twit);
|
observer.OnNext(twit);
|
||||||
}
|
}
|
||||||
@@ -41,10 +41,10 @@ namespace ObserverPattern.Twits
|
|||||||
|
|
||||||
private class Unsubscriber : IDisposable
|
private class Unsubscriber : IDisposable
|
||||||
{
|
{
|
||||||
private List<IObserver<string>> _observers;
|
private List<IObserver<Twit>> _observers;
|
||||||
private IObserver<string> _observer;
|
private IObserver<Twit> _observer;
|
||||||
|
|
||||||
public Unsubscriber(List<IObserver<string>> observers, IObserver<string> observer)
|
public Unsubscriber(List<IObserver<Twit>> observers, IObserver<Twit> observer)
|
||||||
{
|
{
|
||||||
_observers = observers;
|
_observers = observers;
|
||||||
_observer = observer;
|
_observer = observer;
|
||||||
|
|||||||
@@ -8,16 +8,25 @@ namespace ObserverPattern.Twits
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Concrete observer
|
/// Concrete observer
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class TwitUser : IObserver<string>
|
public class TwitUser : IObserver<Twit>, IDisposable
|
||||||
{
|
{
|
||||||
string name;
|
TwitObservable twits;
|
||||||
public TwitUser(string name)
|
public string Name { get; private set; }
|
||||||
|
IDisposable channel;
|
||||||
|
public TwitUser(string name, TwitObservable twits)
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.Name = name;
|
||||||
|
channel = twits.Subscribe(this);
|
||||||
|
this.twits = twits;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Twit(string twit)
|
||||||
|
{
|
||||||
|
twits.AddTwit(new Twit { Emiter = this, Message = twit });
|
||||||
}
|
}
|
||||||
public void OnCompleted()
|
public void OnCompleted()
|
||||||
{
|
{
|
||||||
Console.WriteLine("{0} finished watching twitter", name);
|
Console.WriteLine("{0} finished watching twitter", Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnError(Exception error)
|
public void OnError(Exception error)
|
||||||
@@ -25,9 +34,14 @@ namespace ObserverPattern.Twits
|
|||||||
Console.WriteLine("Error while watching twitter: {0}", error);
|
Console.WriteLine("Error while watching twitter: {0}", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnNext(string value)
|
public void OnNext(Twit value)
|
||||||
{
|
{
|
||||||
Console.WriteLine("{0} just observed that something {1} was tweeted", name, value);
|
Console.WriteLine("{0} just observed that {1} tweeted {2}", Name, value.Emiter.Name, value.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
channel.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user