无废话C#设计模式之二十:Mediator
2009-04-02 08:22:59 来源:WEB开发网l 只需要调整AccountSystem就能调整各大区之间的交互行为,比如我们仅仅希望A和B大区共享信息、C和D大区共享信息,那么对于这种交互策略的改变也需要修改AccountSystem。
l 有利于大区的扩充,有了新的大区后,我们不用在大区中考虑它的交互行为,统一交给AccountSystem去安排。
现在,再来看看引入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;
}
}
}
更多精彩
赞助商链接