diff --git a/BehavioralPatterns.sln b/BehavioralPatterns.sln
index 8eef195..fb6253c 100644
--- a/BehavioralPatterns.sln
+++ b/BehavioralPatterns.sln
@@ -22,6 +22,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MediatorPattern", "src\Medi
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MememntoPattern", "src\MememntoPattern\MememntoPattern.xproj", "{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}"
EndProject
+Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ObserverPattern", "src\ObserverPattern\ObserverPattern.xproj", "{D48DB558-0228-4ACE-88A8-A202E5C57849}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -52,6 +54,10 @@ Global
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8}.Release|Any CPU.Build.0 = Release|Any CPU
+ {D48DB558-0228-4ACE-88A8-A202E5C57849}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {D48DB558-0228-4ACE-88A8-A202E5C57849}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {D48DB558-0228-4ACE-88A8-A202E5C57849}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {D48DB558-0228-4ACE-88A8-A202E5C57849}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -63,5 +69,6 @@ Global
{6F3B7F9A-4D9C-4506-A5F7-3FF5CF4376BD} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{2A63BD0A-9D07-4755-9B16-5DDBEB075B80} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{1DE3FDD3-D025-49D8-BEF4-D5F0688F89E8} = {3820200F-354C-41E6-8F34-B301F5D621C2}
+ {D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2}
EndGlobalSection
EndGlobal
diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs
index 4247641..1c85a69 100644
--- a/src/BehavioralPatterns/Program.cs
+++ b/src/BehavioralPatterns/Program.cs
@@ -4,6 +4,7 @@ using CommandPattern;
using IteratorPattern;
using MediatorPattern;
using MememntoPattern;
+using ObserverPattern;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -15,12 +16,8 @@ namespace BehavioralPatterns
public class Program
{
public static void Main(string[] args)
- {
+ {
- MementoPatternExamples mementoPattern = new MementoPatternExamples();
- mementoPattern.Run();
-
- MediatorPatternExamples.Run();
//Chain of responsibillity
//This is usefull when you have a request and you don't know who should process it
ChainOfResponsibillityExamples.Run();
@@ -35,8 +32,18 @@ namespace BehavioralPatterns
Console.ReadKey();
IteratorPatternExamples.Run();
+
Console.ReadKey();
+ MediatorPatternExamples.Run();
+
+ Console.ReadKey();
+
+ MementoPatternExamples.Run();
+
+
+ ObserverPatternExamples.Run();
+ Console.ReadKey();
}
}
}
diff --git a/src/BehavioralPatterns/project.json b/src/BehavioralPatterns/project.json
index a9167d0..3165efd 100644
--- a/src/BehavioralPatterns/project.json
+++ b/src/BehavioralPatterns/project.json
@@ -16,7 +16,8 @@
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
- }
+ },
+ "ObserverPattern": "1.0.0-*"
},
"frameworks": {
diff --git a/src/MememntoPattern/MementoPatternExamples.cs b/src/MememntoPattern/MementoPatternExamples.cs
index a0ee9ca..0327d44 100644
--- a/src/MememntoPattern/MementoPatternExamples.cs
+++ b/src/MememntoPattern/MementoPatternExamples.cs
@@ -6,17 +6,11 @@ using System.Linq;
using System.Threading.Tasks;
namespace MememntoPattern
-{
- // This project can output the Class library as a NuGet Package.
- // To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
+{
public class MementoPatternExamples
{
- public MementoPatternExamples()
- {
-
- }
- public void Run()
+ public static void Run()
{
Console.WriteLine(GetWhenToUse());
Console.WriteLine(GetActors());
@@ -37,7 +31,7 @@ namespace MememntoPattern
Console.WriteLine(GetPitfalls());
}
- string GetWhenToUse()
+ static string GetWhenToUse()
{
return @"When to use:
When you need to be able to track the state of an object,or/and restore previous states as needed
@@ -46,16 +40,16 @@ Database transactions.
";
}
-
- string GetAlternatives()
+
+ static string GetAlternatives()
{
return @"Alternatives:
Command undo - when operations maybe undone
Iterative Memento - Save the changes, instead of storing the entire state again like GIT";
}
- string GetActors()
+ static string GetActors()
{
return @"Actors:
Originator: object that we want to save. It will create the actual memento.
@@ -63,7 +57,7 @@ Caretaker: keeps the mementos
Memento: (Magic cookie) internal state of the object";
}
- string GetPitfalls()
+ static string GetPitfalls()
{
return @"
Can be expensive
diff --git a/src/ObserverPattern/ObserverPattern.xproj b/src/ObserverPattern/ObserverPattern.xproj
new file mode 100644
index 0000000..3cfb863
--- /dev/null
+++ b/src/ObserverPattern/ObserverPattern.xproj
@@ -0,0 +1,21 @@
+
+
+
+ 14.0
+ $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
+
+
+
+
+ d48db558-0228-4ace-88a8-a202e5c57849
+ ObserverPattern
+ .\obj
+ .\bin\
+ v4.6.1
+
+
+
+ 2.0
+
+
+
diff --git a/src/ObserverPattern/ObserverPatternExamples.cs b/src/ObserverPattern/ObserverPatternExamples.cs
new file mode 100644
index 0000000..c6c640b
--- /dev/null
+++ b/src/ObserverPattern/ObserverPatternExamples.cs
@@ -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();
+ }
+ }
+}
diff --git a/src/ObserverPattern/Properties/AssemblyInfo.cs b/src/ObserverPattern/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..f273d60
--- /dev/null
+++ b/src/ObserverPattern/Properties/AssemblyInfo.cs
@@ -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")]
diff --git a/src/ObserverPattern/Twits/ObservableTwitsExample.cs b/src/ObserverPattern/Twits/ObservableTwitsExample.cs
new file mode 100644
index 0000000..d36add6
--- /dev/null
+++ b/src/ObserverPattern/Twits/ObservableTwitsExample.cs
@@ -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();
+
+
+ }
+ }
+}
diff --git a/src/ObserverPattern/Twits/TwitObservable.cs b/src/ObserverPattern/Twits/TwitObservable.cs
new file mode 100644
index 0000000..72e787e
--- /dev/null
+++ b/src/ObserverPattern/Twits/TwitObservable.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace ObserverPattern.Twits
+{
+ ///
+ /// Concrete observable
+ ///
+ public class TwitObservable : IObservable
+ {
+ List> observers;
+ public TwitObservable()
+ {
+ observers = new List>();
+ }
+ public IDisposable Subscribe(IObserver 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> _observers;
+ private IObserver _observer;
+
+ public Unsubscriber(List> observers, IObserver observer)
+ {
+ _observers = observers;
+ _observer = observer;
+ }
+
+ public void Dispose()
+ {
+ if (!(_observer == null)) _observers.Remove(_observer);
+ }
+ }
+ }
+
+
+}
diff --git a/src/ObserverPattern/Twits/TwitUser.cs b/src/ObserverPattern/Twits/TwitUser.cs
new file mode 100644
index 0000000..bea6315
--- /dev/null
+++ b/src/ObserverPattern/Twits/TwitUser.cs
@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace ObserverPattern.Twits
+{
+ ///
+ /// Concrete observer
+ ///
+ public class TwitUser : IObserver
+ {
+ 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);
+ }
+ }
+}
diff --git a/src/ObserverPattern/project.json b/src/ObserverPattern/project.json
new file mode 100644
index 0000000..ed8608d
--- /dev/null
+++ b/src/ObserverPattern/project.json
@@ -0,0 +1,13 @@
+{
+ "version": "1.0.0-*",
+
+ "dependencies": {
+ "NETStandard.Library": "1.5.0-rc2-24027"
+ },
+
+ "frameworks": {
+ "netstandard1.5": {
+ "imports": "dnxcore50"
+ }
+ }
+}