Add observer pattern
Update memento pattern examples class to have run as static method Update behavioral patterns program.cs to maintain the order
This commit is contained in:
21
src/ObserverPattern/ObserverPattern.xproj
Normal file
21
src/ObserverPattern/ObserverPattern.xproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>d48db558-0228-4ace-88a8-a202e5c57849</ProjectGuid>
|
||||
<RootNamespace>ObserverPattern</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
18
src/ObserverPattern/ObserverPatternExamples.cs
Normal file
18
src/ObserverPattern/ObserverPatternExamples.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using ObserverPattern.Twits;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ObserverPattern
|
||||
{
|
||||
|
||||
public class ObserverPatternExamples
|
||||
{
|
||||
public static void Run()
|
||||
{
|
||||
ObservableTwitsExample obsTwits = new ObservableTwitsExample();
|
||||
obsTwits.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/ObserverPattern/Properties/AssemblyInfo.cs
Normal file
19
src/ObserverPattern/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Hewlett-Packard Company")]
|
||||
[assembly: AssemblyProduct("ObserverPattern")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("d48db558-0228-4ace-88a8-a202e5c57849")]
|
||||
31
src/ObserverPattern/Twits/ObservableTwitsExample.cs
Normal file
31
src/ObserverPattern/Twits/ObservableTwitsExample.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ObserverPattern.Twits
|
||||
{
|
||||
public class ObservableTwitsExample
|
||||
{
|
||||
public void Run()
|
||||
{
|
||||
TwitObservable observable = new TwitObservable();
|
||||
|
||||
TwitUser t100 = new TwitUser("t100");
|
||||
TwitUser r2d2 = new TwitUser("r2d2");
|
||||
|
||||
var t100Subscription = observable.Subscribe(t100);
|
||||
var r2d2Subscription = observable.Subscribe(r2d2);
|
||||
|
||||
observable.AddTwit("El chupacapra");
|
||||
|
||||
t100Subscription.Dispose();
|
||||
|
||||
observable.AddTwit("Vamos vamos mi amor");
|
||||
|
||||
observable.ItsGoingHomeTime();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
61
src/ObserverPattern/Twits/TwitObservable.cs
Normal file
61
src/ObserverPattern/Twits/TwitObservable.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ObserverPattern.Twits
|
||||
{
|
||||
/// <summary>
|
||||
/// Concrete observable
|
||||
/// </summary>
|
||||
public class TwitObservable : IObservable<string>
|
||||
{
|
||||
List<IObserver<string>> observers;
|
||||
public TwitObservable()
|
||||
{
|
||||
observers = new List<IObserver<string>>();
|
||||
}
|
||||
public IDisposable Subscribe(IObserver<string> observer)
|
||||
{
|
||||
if (!observers.Contains(observer))
|
||||
observers.Add(observer);
|
||||
|
||||
return new Unsubscriber(observers, observer);
|
||||
}
|
||||
|
||||
public void AddTwit(string twit)
|
||||
{
|
||||
foreach (var observer in observers)
|
||||
{
|
||||
observer.OnNext(twit);
|
||||
}
|
||||
}
|
||||
|
||||
public void ItsGoingHomeTime()
|
||||
{
|
||||
foreach (var observer in observers)
|
||||
{
|
||||
observer.OnCompleted();
|
||||
}
|
||||
}
|
||||
|
||||
private class Unsubscriber : IDisposable
|
||||
{
|
||||
private List<IObserver<string>> _observers;
|
||||
private IObserver<string> _observer;
|
||||
|
||||
public Unsubscriber(List<IObserver<string>> observers, IObserver<string> observer)
|
||||
{
|
||||
_observers = observers;
|
||||
_observer = observer;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!(_observer == null)) _observers.Remove(_observer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
33
src/ObserverPattern/Twits/TwitUser.cs
Normal file
33
src/ObserverPattern/Twits/TwitUser.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ObserverPattern.Twits
|
||||
{
|
||||
/// <summary>
|
||||
/// Concrete observer
|
||||
/// </summary>
|
||||
public class TwitUser : IObserver<string>
|
||||
{
|
||||
string name;
|
||||
public TwitUser(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
public void OnCompleted()
|
||||
{
|
||||
Console.WriteLine("{0} finished watching twitter", name);
|
||||
}
|
||||
|
||||
public void OnError(Exception error)
|
||||
{
|
||||
Console.WriteLine("Error while watching twitter: {0}", error);
|
||||
}
|
||||
|
||||
public void OnNext(string value)
|
||||
{
|
||||
Console.WriteLine("{0} just observed that something {1} was tweeted", name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/ObserverPattern/project.json
Normal file
13
src/ObserverPattern/project.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.5.0-rc2-24027"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"netstandard1.5": {
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user