Skip to content

Commit 3fe8d0c

Browse files
committed
mediator pattern example
1 parent 6c73898 commit 3fe8d0c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandPattern", "CommandPa
3333
EndProject
3434
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{BB1AE146-315A-44F8-A90B-77E386C65678}"
3535
EndProject
36+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediatorPattern", "MediatorPattern\MediatorPattern.csproj", "{A6278444-3DDF-4175-996C-361E536668FC}"
37+
EndProject
3638
Global
3739
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3840
Debug|Any CPU = Debug|Any CPU
@@ -99,6 +101,10 @@ Global
99101
{BB1AE146-315A-44F8-A90B-77E386C65678}.Debug|Any CPU.Build.0 = Debug|Any CPU
100102
{BB1AE146-315A-44F8-A90B-77E386C65678}.Release|Any CPU.ActiveCfg = Release|Any CPU
101103
{BB1AE146-315A-44F8-A90B-77E386C65678}.Release|Any CPU.Build.0 = Release|Any CPU
104+
{A6278444-3DDF-4175-996C-361E536668FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
105+
{A6278444-3DDF-4175-996C-361E536668FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
106+
{A6278444-3DDF-4175-996C-361E536668FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
107+
{A6278444-3DDF-4175-996C-361E536668FC}.Release|Any CPU.Build.0 = Release|Any CPU
102108
EndGlobalSection
103109
GlobalSection(SolutionProperties) = preSolution
104110
HideSolutionNode = FALSE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

MediatorPattern/Program.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
3+
namespace MediatorPattern
4+
{
5+
interface IChatRoomMediator
6+
{
7+
void ShowMessage(User user, string message);
8+
}
9+
10+
//Mediator
11+
class ChatRoom : IChatRoomMediator
12+
{
13+
public void ShowMessage(User user, string message)
14+
{
15+
Console.WriteLine("{0} [{1}]:{2}", DateTime.Now.ToString("MMMM dd, H:mm"), user.GetName(), message);
16+
}
17+
}
18+
19+
class User
20+
{
21+
private string mName;
22+
private IChatRoomMediator mChatRoom;
23+
24+
public User(string name, IChatRoomMediator chatroom)
25+
{
26+
mChatRoom = chatroom;
27+
mName = name;
28+
}
29+
30+
public string GetName()
31+
{
32+
return mName;
33+
}
34+
35+
public void Send(string message)
36+
{
37+
mChatRoom.ShowMessage(this, message);
38+
}
39+
}
40+
41+
42+
class Program
43+
{
44+
static void Main(string[] args)
45+
{
46+
var mediator = new ChatRoom();
47+
48+
var john = new User("John", mediator);
49+
var jane = new User("Jane", mediator);
50+
51+
john.Send("Hi there!");
52+
jane.Send("Hey!");
53+
54+
//April 14, 20:05[John]:Hi there!
55+
//April 14, 20:05[Jane]:Hey!
56+
57+
Console.ReadLine();
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)