ze poker hand checker

This commit is contained in:
Gardient
2016-07-01 00:31:12 +03:00
committed by Petrutiu Mihai
parent 0a6d290c3e
commit b5997ced99
34 changed files with 612 additions and 183 deletions

View File

@@ -1,5 +1,4 @@
using ObserverPattern.StockUpdateEvents;
using ObserverPattern.Twits;
using ObserverPattern.Twits;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -12,78 +11,8 @@ namespace ObserverPattern
{
public static void Run()
{
Console.WriteLine(GetDescription());
Console.WriteLine(GetActors());
Console.WriteLine(IsThisPubSub());
StockUpdateEventsExample stockExample = new StockUpdateEventsExample();
stockExample.RunSimple();
GoToNextStep();
Console.WriteLine(GetLapsedLinstenerProblem());
GoToNextStep();
Console.WriteLine("Same business logic using events combined with RX library");
stockExample.RunReactiveWithEvents();
GoToNextStep();
Console.WriteLine("Same business logic using RX library");
stockExample.RunReactive();
GoToNextStep();
ObservableTwitsExample obsTwits = new ObservableTwitsExample();
obsTwits.Run();
GoToNextStep();
}
private static string GetDescription()
{
return @"
The observer pattern is a software design pattern in which an object, called the subject,
maintains a list of its dependents, called observers, and notifies them automatically of any state changes,
usually by calling one of their methods. ";
}
private static string IsThisPubSub()
{
return @"
Observer/Observable pattern is mostly implemented in a synchronous way,
i.e. the observable calls the appropriate method of all its observers when some event occurs.
The Publisher/Subscriber pattern is mostly implemented in an asynchronous way (using message queue).
In the Observer/Observable pattern, the observers are aware of the observable.
Whereas, in Publisher/Subscriber, publishers and subscribers don't need to know each other.
They simply communicate with the help of message queues.
";
}
private static string GetActors()
{
return @"
Subject -> Interface/Abstract Notifies interested observers when an event occurs
Concrete Subject -> Implementation of Subject
Observer -> Interface/Abstract class -> Registers to a subject, to be notified when a specific event happens
Concrete Observer -> Implementation of the observer
";
}
private static string GetLapsedLinstenerProblem()
{
return @"
The leak happens when a listener fails to unsubscribe from the publisher when it no longer needs to listen.
Consequently, the publisher still holds a reference to the observer which prevents it from being garbage collected
— including all other objects it is referring to — for as long as the publisher is alive, which could be until the end of the application.
This causes not only a memory leak, but also a performance degradation with an 'uninterested' observer receiving and acting on unwanted events";
}
private static void GoToNextStep()
{
Console.ReadKey();
Console.Clear();
}
}
}

View File

@@ -11,16 +11,16 @@ namespace ObserverPattern.Twits
{
TwitObservable observable = new TwitObservable();
using (TwitUser t100 = new TwitUser("t100", observable))
using (TwitUser r2d2 = new TwitUser("R2-D2", observable))
{
t100.Twit("El chupacapra - BOOM BOOM");
TwitUser t100 = new TwitUser("t100", observable);
TwitUser r2d2 = new TwitUser("R2-D2", observable);
r2d2.Twit("Vamos vamos mi amor");
t100.Twit("El chupacapra - BOOM BOOM");
observable.ItsGoingHomeTime();
}
r2d2.Twit("Vamos vamos mi amor");
t100.Dispose();
observable.ItsGoingHomeTime();
}
}
}

View File

@@ -20,7 +20,7 @@ namespace ObserverPattern.Twits
if (!observers.Contains(observer))
observers.Add(observer);
return new Unsubscriber<Twit>(observers, observer);
return new Unsubscriber(observers, observer);
}
public void AddTwit(Twit twit)
@@ -37,6 +37,23 @@ namespace ObserverPattern.Twits
{
observer.OnCompleted();
}
}
private class Unsubscriber : IDisposable
{
private List<IObserver<Twit>> _observers;
private IObserver<Twit> _observer;
public Unsubscriber(List<IObserver<Twit>> observers, IObserver<Twit> observer)
{
_observers = observers;
_observer = observer;
}
public void Dispose()
{
if (!(_observer == null)) _observers.Remove(_observer);
}
}
}

View File

@@ -1,9 +1,8 @@
{
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Reactive": "3.0.0"
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {