Skip to content

Commit 5caeb33

Browse files
committed
Mediator pattern implemented.
1 parent ddb1200 commit 5caeb33

11 files changed

+351
-0
lines changed

Behavioral/Mediator/BookButton.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Windows.Controls;
2+
3+
namespace Mediator {
4+
5+
public class BookButton : Button, IColleague {
6+
7+
private readonly IMediator _mediator;
8+
9+
public BookButton(IMediator mediator) {
10+
this.Content = "Book";
11+
this._mediator = mediator;
12+
this._mediator.RegisterBook(this);
13+
}
14+
15+
public void Execute() {
16+
this._mediator.Book();
17+
}
18+
}
19+
}

Behavioral/Mediator/DisplayLabel.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Windows;
2+
using System.Windows.Controls;
3+
4+
namespace Mediator {
5+
6+
public class DisplayLabel : Label {
7+
8+
private readonly IMediator _mediator;
9+
10+
public DisplayLabel(IMediator mediator) {
11+
this.Content = "Starting ...";
12+
this.FontSize = 24;
13+
//this.FontWeight = new FontWeight();
14+
this._mediator = mediator;
15+
this._mediator.RegisterDisplay(this);
16+
}
17+
}
18+
}

Behavioral/Mediator/IColleague.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Mediator {
2+
3+
/// <summary>
4+
/// Colleague interface
5+
/// </summary>
6+
public interface IColleague {
7+
8+
void Execute();
9+
}
10+
}

Behavioral/Mediator/IMediator.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Windows.Controls;
2+
3+
namespace Mediator {
4+
5+
/// <summary>
6+
/// Mediator interface
7+
/// </summary>
8+
public interface IMediator {
9+
10+
void Book();
11+
12+
void View();
13+
14+
void Search();
15+
16+
void RegisterView(ViewButton viewButton);
17+
18+
void RegisterSearch(SearchButton searchButton);
19+
20+
void RegisterBook(BookButton bookButton);
21+
22+
void RegisterDisplay(DisplayLabel displayLabel);
23+
24+
}
25+
}

Behavioral/Mediator/Mediator.cs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace Mediator {
2+
3+
class Mediator : IMediator {
4+
5+
private ViewButton _viewButton;
6+
private SearchButton _searchButton;
7+
private BookButton _bookButton;
8+
private DisplayLabel _displayLabel;
9+
10+
public void Book() {
11+
this._bookButton.IsEnabled = false;
12+
this._viewButton.IsEnabled = true;
13+
this._searchButton.IsEnabled = true;
14+
this._displayLabel.Content = "Booking ...";
15+
}
16+
17+
public void View() {
18+
this._bookButton.IsEnabled = true;
19+
this._viewButton.IsEnabled = false;
20+
this._searchButton.IsEnabled = true;
21+
this._displayLabel.Content = "Viewing ...";
22+
}
23+
24+
public void Search() {
25+
this._bookButton.IsEnabled = true;
26+
this._viewButton.IsEnabled = true;
27+
this._searchButton.IsEnabled = false;
28+
this._displayLabel.Content = "Searching ...";
29+
}
30+
31+
public void RegisterView(ViewButton viewButton) {
32+
this._viewButton = viewButton;
33+
}
34+
35+
public void RegisterSearch(SearchButton searchButton) {
36+
this._searchButton = searchButton;
37+
}
38+
39+
public void RegisterBook(BookButton bookButton) {
40+
this._bookButton = bookButton;
41+
}
42+
43+
public void RegisterDisplay(DisplayLabel displayLabel) {
44+
this._displayLabel = displayLabel;
45+
}
46+
}
47+
}

Behavioral/Mediator/Mediator.csproj

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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>{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Mediator</RootNamespace>
12+
<AssemblyName>Mediator</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+
<ItemGroup>
37+
<Reference Include="PresentationCore" />
38+
<Reference Include="PresentationFramework" />
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Windows.Presentation" />
42+
<Reference Include="System.Xaml" />
43+
<Reference Include="System.Xml.Linq" />
44+
<Reference Include="System.Data.DataSetExtensions" />
45+
<Reference Include="Microsoft.CSharp" />
46+
<Reference Include="System.Data" />
47+
<Reference Include="System.Xml" />
48+
<Reference Include="WindowsBase" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<Compile Include="BookButton.cs" />
52+
<Compile Include="SearchButton.cs" />
53+
<Compile Include="ViewButton.cs" />
54+
<Compile Include="MediatorDemo.cs" />
55+
<Compile Include="IColleague.cs" />
56+
<Compile Include="IMediator.cs" />
57+
<Compile Include="DisplayLabel.cs" />
58+
<Compile Include="Mediator.cs" />
59+
<Compile Include="Properties\AssemblyInfo.cs" />
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
63+
Other similar extension points exist, see Microsoft.Common.targets.
64+
<Target Name="BeforeBuild">
65+
</Target>
66+
<Target Name="AfterBuild">
67+
</Target>
68+
-->
69+
</Project>

Behavioral/Mediator/MediatorDemo.cs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Windows;
6+
using System.Windows.Controls;
7+
8+
namespace Mediator {
9+
10+
class MediatorDemo : Window {
11+
12+
private readonly IMediator _mediator = new Mediator();
13+
14+
public MediatorDemo() {
15+
16+
var rootView = new Grid();
17+
rootView.RowDefinitions.Add(new RowDefinition());
18+
rootView.RowDefinitions.Add(new RowDefinition());
19+
rootView.ColumnDefinitions.Add(new ColumnDefinition());
20+
rootView.ColumnDefinitions.Add(new ColumnDefinition());
21+
rootView.ColumnDefinitions.Add(new ColumnDefinition());
22+
23+
var viewButton = new ViewButton(this._mediator);
24+
viewButton.Click += this.OnViewClick;
25+
Grid.SetColumn(viewButton, 0);
26+
Grid.SetRow(viewButton, 0);
27+
rootView.Children.Add(viewButton);
28+
29+
var bookButton = new BookButton(this._mediator);
30+
bookButton.Click += this.OnViewClick;
31+
Grid.SetColumn(bookButton, 1);
32+
Grid.SetRow(bookButton, 0);
33+
rootView.Children.Add(bookButton);
34+
35+
var searchButton = new SearchButton(this._mediator);
36+
searchButton.Click += this.OnViewClick;
37+
Grid.SetColumn(searchButton, 2);
38+
Grid.SetRow(searchButton, 0);
39+
rootView.Children.Add(searchButton);
40+
41+
42+
var displayLabel = new DisplayLabel(this._mediator);
43+
Grid.SetColumn(displayLabel, 0);
44+
Grid.SetRow(displayLabel, 1);
45+
Grid.SetColumnSpan(displayLabel, 3);
46+
rootView.Children.Add(displayLabel);
47+
48+
this.Content = rootView;
49+
50+
this.Width = 400;
51+
this.Height = 200;
52+
this.Title = "Mediator Demo";
53+
}
54+
55+
private void OnViewClick(object sender, RoutedEventArgs routedEventArgs) {
56+
var colleague = (IColleague)sender;
57+
colleague.Execute();
58+
}
59+
60+
[STAThread]
61+
static void Main(string[] args) {
62+
var app = new Application();
63+
app.Startup += AppOnStartup;
64+
app.Run();
65+
}
66+
67+
private static void AppOnStartup(object sender, StartupEventArgs startupEventArgs) {
68+
Application.Current.MainWindow = new MediatorDemo();
69+
Application.Current.MainWindow.Show();
70+
}
71+
}
72+
}
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("Mediator")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Mediator")]
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("2bac36b6-5ac6-43c2-8785-4d351ba7792b")]
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")]

Behavioral/Mediator/SearchButton.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Windows.Controls;
2+
3+
namespace Mediator {
4+
5+
public class SearchButton : Button, IColleague {
6+
7+
private readonly IMediator _mediator;
8+
9+
public SearchButton(IMediator mediator) {
10+
this.Content = "Search";
11+
this._mediator = mediator;
12+
this._mediator.RegisterSearch(this);
13+
}
14+
15+
public void Execute() {
16+
this._mediator.Search();
17+
}
18+
}
19+
}

Behavioral/Mediator/ViewButton.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Windows.Controls;
2+
3+
namespace Mediator {
4+
5+
public class ViewButton : Button, IColleague {
6+
7+
private readonly IMediator _mediator;
8+
9+
public ViewButton(IMediator mediator) {
10+
this.Content = "View";
11+
this._mediator = mediator;
12+
this._mediator.RegisterView(this);
13+
}
14+
15+
public void Execute() {
16+
this._mediator.View();
17+
}
18+
}
19+
}

DesignPatterns.sln

+17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interpreter", "Behavioral\I
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Iterator", "Behavioral\Iterator\Iterator.csproj", "{2A37AC43-5F17-4774-B9BE-8D5008F41F43}"
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mediator", "Behavioral\Mediator\Mediator.csproj", "{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfApp", "R:\WpfApp\WpfApp.csproj", "{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}"
17+
EndProject
1418
Global
1519
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1620
Debug|Any CPU = Debug|Any CPU
@@ -45,6 +49,18 @@ Global
4549
{2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Release|Any CPU.ActiveCfg = Release|x86
4650
{2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Release|x86.ActiveCfg = Release|x86
4751
{2A37AC43-5F17-4774-B9BE-8D5008F41F43}.Release|x86.Build.0 = Release|x86
52+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Debug|Any CPU.ActiveCfg = Debug|x86
53+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Debug|x86.ActiveCfg = Debug|x86
54+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Debug|x86.Build.0 = Debug|x86
55+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|Any CPU.ActiveCfg = Release|x86
56+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|x86.ActiveCfg = Release|x86
57+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6}.Release|x86.Build.0 = Release|x86
58+
{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}.Debug|Any CPU.ActiveCfg = Debug|x86
59+
{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}.Debug|x86.ActiveCfg = Debug|x86
60+
{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}.Debug|x86.Build.0 = Debug|x86
61+
{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}.Release|Any CPU.ActiveCfg = Release|x86
62+
{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}.Release|x86.ActiveCfg = Release|x86
63+
{CDE97CC2-CF6A-4FF9-B711-97A6B70C4AD8}.Release|x86.Build.0 = Release|x86
4864
EndGlobalSection
4965
GlobalSection(SolutionProperties) = preSolution
5066
HideSolutionNode = FALSE
@@ -54,5 +70,6 @@ Global
5470
{65459173-442A-4E0D-B758-3B9EDCE54D75} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
5571
{6007D4E1-1676-4FBC-816D-1B19F6829F22} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
5672
{2A37AC43-5F17-4774-B9BE-8D5008F41F43} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
73+
{9D3B3EAE-1446-4896-8800-C8EA82C3BFD6} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
5774
EndGlobalSection
5875
EndGlobal

0 commit comments

Comments
 (0)