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:
Petrutiu Mihai
2016-06-23 21:39:13 +03:00
parent 2894347140
commit c66816b938
11 changed files with 224 additions and 19 deletions

View File

@@ -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

View File

@@ -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();
}
}
}

View File

@@ -16,7 +16,8 @@
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"ObserverPattern": "1.0.0-*"
},
"frameworks": {

View File

@@ -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

View 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>

View 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();
}
}
}

View 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")]

View 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();
}
}
}

View 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);
}
}
}
}

View 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);
}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50"
}
}
}