Skip to content

Commit 429f79b

Browse files
committed
add observer pattern
1 parent 54b57fe commit 429f79b

File tree

8 files changed

+202
-0
lines changed

8 files changed

+202
-0
lines changed

Behavioral/Observer/Client.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Observer {
7+
8+
class Client {
9+
10+
static void Main(string[] args) {
11+
var subject = new ConcreteSubject();
12+
13+
subject.Attach(new ConcreteObserver(subject));
14+
subject.Attach(new ConcreteObserver(subject));
15+
16+
subject.State = "State1";
17+
subject.Notify();
18+
19+
subject.State = "State2";
20+
subject.Notify();
21+
22+
Console.ReadKey();
23+
}
24+
}
25+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Observer {
4+
5+
public class ConcreteObserver : IObserver {
6+
7+
private readonly ConcreteSubject _subject;
8+
9+
public ConcreteObserver(ConcreteSubject subject) {
10+
this._subject = subject;
11+
}
12+
13+
public void Update() {
14+
Console.WriteLine("Observer: subject state updated to {0} .", this._subject.State);
15+
}
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Collections.Generic;
2+
3+
namespace Observer {
4+
5+
public class ConcreteSubject : ISubject {
6+
7+
private readonly IList<IObserver> _observers = new List<IObserver>();
8+
9+
public string State {
10+
get;
11+
set;
12+
}
13+
14+
public void Attach(IObserver observer) {
15+
if (!this._observers.Contains(observer)) {
16+
this._observers.Add(observer);
17+
}
18+
}
19+
20+
public void Detach(IObserver observer) {
21+
if (this._observers.Contains(observer)) {
22+
this._observers.Remove(observer);
23+
}
24+
}
25+
26+
public void Notify() {
27+
foreach (var observer in _observers) {
28+
observer.Update();
29+
}
30+
}
31+
}
32+
}

Behavioral/Observer/IObserver.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Observer {
2+
3+
public interface IObserver {
4+
5+
void Update();
6+
7+
}
8+
}

Behavioral/Observer/ISubject.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Observer {
2+
3+
public interface ISubject {
4+
5+
void Attach(IObserver observer);
6+
7+
void Detach(IObserver observer);
8+
9+
void Notify();
10+
}
11+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{2823F21D-D474-447D-9C8B-74B29D0D9B35}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Observer</RootNamespace>
12+
<AssemblyName>Observer</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<PropertyGroup>
37+
<StartupObject>Observer.Client</StartupObject>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Client.cs" />
50+
<Compile Include="ConcreteObserver.cs" />
51+
<Compile Include="ConcreteSubject.cs" />
52+
<Compile Include="IObserver.cs" />
53+
<Compile Include="ISubject.cs" />
54+
<Compile Include="Properties\AssemblyInfo.cs" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
58+
Other similar extension points exist, see Microsoft.Common.targets.
59+
<Target Name="BeforeBuild">
60+
</Target>
61+
<Target Name="AfterBuild">
62+
</Target>
63+
-->
64+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Observer")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Observer")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("6667d06c-0f67-48a7-88f6-14b5aa791005")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "Behavioral\Medi
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memento", "Behavioral\Memento\Memento.csproj", "{C404876C-B04B-451F-B159-2C0CF8CB861B}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Observer", "Behavioral\Observer\Observer.csproj", "{2823F21D-D474-447D-9C8B-74B29D0D9B35}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -61,6 +63,12 @@ Global
6163
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|Any CPU.ActiveCfg = Release|x86
6264
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|x86.ActiveCfg = Release|x86
6365
{C404876C-B04B-451F-B159-2C0CF8CB861B}.Release|x86.Build.0 = Release|x86
66+
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Debug|Any CPU.ActiveCfg = Debug|x86
67+
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Debug|x86.ActiveCfg = Debug|x86
68+
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Debug|x86.Build.0 = Debug|x86
69+
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|Any CPU.ActiveCfg = Release|x86
70+
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|x86.ActiveCfg = Release|x86
71+
{2823F21D-D474-447D-9C8B-74B29D0D9B35}.Release|x86.Build.0 = Release|x86
6472
EndGlobalSection
6573
GlobalSection(SolutionProperties) = preSolution
6674
HideSolutionNode = FALSE
@@ -72,5 +80,6 @@ Global
7280
{2A37AC43-5F17-4774-B9BE-8D5008F41F43} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
7381
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
7482
{C404876C-B04B-451F-B159-2C0CF8CB861B} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
83+
{2823F21D-D474-447D-9C8B-74B29D0D9B35} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
7584
EndGlobalSection
7685
EndGlobal

0 commit comments

Comments
 (0)