revert the automerge changes

This commit is contained in:
Gardient
2016-07-04 14:39:26 +03:00
committed by Petrutiu Mihai
parent 3eee148d76
commit 21327fc73f
4 changed files with 84 additions and 29 deletions

View File

@@ -1,4 +1,5 @@
using ObserverPattern.Twits;
using ObserverPattern.StockUpdateEvents;
using ObserverPattern.Twits;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -11,8 +12,78 @@ 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();
TwitUser t100 = new TwitUser("t100", observable);
TwitUser r2d2 = new TwitUser("R2-D2", observable);
using (TwitUser t100 = new TwitUser("t100", observable))
using (TwitUser r2d2 = new TwitUser("R2-D2", observable))
{
t100.Twit("El chupacapra - BOOM BOOM");
t100.Twit("El chupacapra - BOOM BOOM");
r2d2.Twit("Vamos vamos mi amor");
r2d2.Twit("Vamos vamos mi amor");
observable.ItsGoingHomeTime();
}
t100.Dispose();
observable.ItsGoingHomeTime();
}
}
}

View File

@@ -20,7 +20,7 @@ namespace ObserverPattern.Twits
if (!observers.Contains(observer))
observers.Add(observer);
return new Unsubscriber(observers, observer);
return new Unsubscriber<Twit>(observers, observer);
}
public void AddTwit(Twit twit)
@@ -38,23 +38,6 @@ 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,8 +1,9 @@
{
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
"NETStandard.Library": "1.6.0",
"System.Reactive": "3.0.0"
},
"frameworks": {