現在,再來看看引入AccountSystem後的通訊:
l 網站調用AccountSystem的注冊方法(1)
l AccountSystem調用A、B和C大區的注冊方法(2)
l A、B和C大區的充值方法調用AccountSystem的充值方法(3)
l A、B和C大區的消費方法調用AccountSystem的充值方法(4)
l AccountSystem的充值方法調用A、B和C大區的專有充值方法(只針對本大區的充值)(5)
l AccountSystem的充值方法調用A、B和C大區的專有消費方法(只針對本大區的消費)(6)
至此,你已經實現了中介者模式。你可能會覺得,(1)和(2)非常類似門面模式,沒錯,它確實就是門面模式,而有了(3)~(6)的行為,AccountSystem也就是一個中介者的角色了。
示例代碼
using System;
using System.Collections.Generic;
using System.Text;
namespace MediatorExample
{
class Program
{
static void Main(string[] args)
{
AccountSystem accountSystem = new AccountSystem();
GameSystem gameArea1 = new GameArea1(accountSystem);
GameSystem gameArea2 = new GameArea2(accountSystem);
accountSystem.RegisterGameArea(gameArea1);
accountSystem.RegisterGameArea(gameArea2);
string userName = "aaa";
accountSystem.CreateAccount(userName);
gameArea1.Recharge(userName, 200);
gameArea2.Consume(userName, 50);
accountSystem.QueryBalance(userName);
}
}
class AccountSystem
{
private Dictionary<string, int> userBalance = new Dictionary<string, int>();
private List<GameSystem> gameAreaList = new List<GameSystem>();
public void RegisterGameArea(GameSystem gs)
{
gameAreaList.Add(gs);
}
public void CreateAccount(string userName)
{
userBalance.Add(userName, 0);
foreach (GameSystem gs in gameAreaList)
gs.CreateAccountSelf(userName);
}
public void Recharge(string userName, int amount)
{
if (userBalance.ContainsKey(userName))
{
bool ok = true;
foreach (GameSystem gs in gameAreaList)
ok = gs.RechargeSelf(userName, amount);
if (ok)
userBalance[userName] += amount;
}
}
public void Consume(string userName, int amount)
{
if (userBalance.ContainsKey(userName))
{
bool ok = true;
foreach (GameSystem gs in gameAreaList)
ok = gs.ConsumeSelf(userName, amount);
if (ok)
userBalance[userName] -= amount;
}
}
public void QueryBalance(string userName)
{
Console.WriteLine("Your balance is " + userBalance[userName]);
}
}
abstract class GameSystem
{
private AccountSystem accountSystem;
protected Dictionary<string, int> userBalance = new Dictionary<string, int>();
public GameSystem(AccountSystem accountSystem)
{
this.accountSystem = accountSystem;
}
internal virtual bool CreateAccountSelf(string userName)
{
userBalance.Add(userName, 0);
return true;
}
internal virtual bool RechargeSelf(string userName, int amount)
{
if (userBalance.ContainsKey(userName))
userBalance[userName] += amount;
return true;
}
internal virtual bool ConsumeSelf(string userName, int amount)
{
if (userBalance.ContainsKey(userName))
userBalance[userName] -= amount;
return true;
}
public void Recharge(string userName, int amount)
{
accountSystem.Recharge(userName, amount);
}
public void Consume(string userName, int amount)
{
accountSystem.Consume(userName, amount);
}
}
class GameArea1 : GameSystem
{
public GameArea1(AccountSystem accountSystem) : base(accountSystem) { }
internal override bool CreateAccountSelf(string userName)
{
Console.WriteLine(userName + " Registered in GameAre1");
return base.CreateAccountSelf(userName);
}
internal override bool RechargeSelf(string userName, int amount)
{
base.RechargeSelf(userName, amount);
Console.WriteLine(userName + "'s amount in GameArea1 is " + userBalance[userName]);
return true;
}
internal override bool ConsumeSelf(string userName, int amount)
{
base.ConsumeSelf(userName, amount);
Console.WriteLine(userName + "'s amount in GameArea1 is " + userBalance[userName]);
return true;
}
}
class GameArea2 : GameSystem
{
public GameArea2(AccountSystem accountSystem) : base(accountSystem) { }
internal override bool CreateAccountSelf(string userName)
{
Console.WriteLine(userName + " Registered in GameAre2");
return base.CreateAccountSelf(userName);
}
internal override bool RechargeSelf(string userName, int amount)
{
base.RechargeSelf(userName, amount);
Console.WriteLine(userName + "'s amount in GameArea2 is " + userBalance[userName]);
return true;
}
internal override bool ConsumeSelf(string userName, int amount)
{
base.ConsumeSelf(userName, amount);
Console.WriteLine(userName + "'s amount in GameArea2 is " + userBalance[userName]);
return true;
}
}
}