`
lovnet
  • 浏览: 6704610 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

(原创)无废话C#设计模式之二十:Mediator

阅读更多

无废话C#设计模式之二十:Mediator

意图

用一个中介对象来封装一系列对象的交互。中介者使得各对象不需要显式相互引用,从而使其松散耦合,而且可以独立地改变它们之间的交互。

场景

我们知道,一个网络游戏往往有很多大区。每一个大区可以是一组服务器,也可以是多组服务器,在这里假设一个大区是一组服务器。为了效率,一般每个大区都会有一个数据库,玩家的创建角色、充值、消费行为只是在这一个大区中有效。现在公司有了新的需求,那就是玩家的一些信息能在多个大区中共享。比如,在注册的时候就把玩家的账户信息写入多个信息共享的大区,玩家在某个大区中充值需要“通知”其它大区修改账户余额,玩家在某个大区中消费也需要“通知”其它大区修改账户余额。

如果我们现在有ABC三个大区,下面的方法可以实现需求:

l 网站的注册方法调用ABC大区的注册方法

l A大区的充值方法调用BC的充值方法

l B大区的充值方法调用AC的充值方法

l C大区的充值方法调用AB的充值方法

l A大区的消费方法调用BC的充值方法

l B大区的消费方法调用AC的充值方法

l C大区的消费方法调用AB的充值方法

我想,没有人会这么做吧。你肯定会想到在一个统一的地方去维护所有大区的信息,任何一个大区的行为不直接和其它大区的行为关联,它们所有的行为都提交到一个统一的地方(假设它是AccountSystem)去处理。这么做有几个好处:

l 各大区不需要关心还有哪些其它的大区,它只直接和AccountSystem对话。

l 只需要调整AccountSystem就能调整各大区之间的交互行为,比如我们仅仅希望AB大区共享信息、CD大区共享信息,那么对于这种交互策略的改变也需要修改AccountSystem

l 有利于大区的扩充,有了新的大区后,我们不用在大区中考虑它的交互行为,统一交给AccountSystem去安排。

现在,再来看看引入AccountSystem后的通讯:

l 网站调用AccountSystem的注册方法(1

l AccountSystem调用ABC大区的注册方法(2

l ABC大区的充值方法调用AccountSystem的充值方法(3

l ABC大区的消费方法调用AccountSystem的充值方法(4

l AccountSystem的充值方法调用ABC大区的专有充值方法(只针对本大区的充值)(5

l AccountSystem的充值方法调用ABC大区的专有消费方法(只针对本大区的消费)(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;

}

<span lan

分享到:
评论

相关推荐

    C#面向对象设计模式纵横谈(视频与源码)

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 C#面向对象设计模式纵横谈(18):(行为型模式) Iterator 迭代器模式 C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式 C#...

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式

    C#设计模式_设计模式_C#_

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)

    设计模式之中介者模式(Mediator)

    中介者模式(Mediator) 用意:用一个中介对象来封装一系列对象间的交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。

    C#23种设计模式_示例源代码及PDF

    解释器模式将描述怎样 在 有了一个简单的文法后, 使用模式设计解释这些语句。 在解释器模式里面提到的语言是指任 何解释器对象能够解释的任何组合。在解释器模式中需要定义一个代表 文法的命令类的等 级结构,也...

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 (Level 300)

    C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 (Level 300)

    36种最新设计模式整理

    36种最新设计模式整理 Design Pattern: Simple Factory 模式 Design Pattern: Abstract Factory 模式 Design Pattern: Builder 模式 Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 ...

    JAVA设计模式chm文档

    创建模式: 设计模式之Factory 设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) 结构模式: ...设计模式之Mediator(中介者) 设计模式之Interpreter(解释器) 设计模式之Visitor

    C#设计模式(23种设计模式)

    中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式...

    C#版 24种设计模式

    提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式(Mediator Pattern) 装饰模式...

    设计模式文档 chm

    设计模式参考文档 创建模式: 设计模式之Factory 设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) ...设计模式之Mediator(中介者) 设计模式之Interpreter(解释器) 设计模式之Visitor

    C#23种设计模式

    │ │ └─C#设计模式(6)——原型模式(Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.Adapter...

    设计模式代码——c#

    C#设计模式(23种设计模式) 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器...

    C++设计模式课件17_Mediator_中介者.pdf

    C++设计模式课件17_Mediator_中介者.pdf

    设计模式之中介者模式(Mediator Pattern)

    用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

    运用Mediator设计模式的登录对话框

    这是一个运用了中介者设计模式的登录对话框练习题。 该案例改编自结城浩《设计模式-java语言中的应用》一书。 This is a program about a login dialog, wich try to illustrate the design pattern of Mediator. ...

    C++ Mediator模式

    23种设计模式之十八(行为模式)Mediator模式

    mediator模式

    用一个中介者对象来封装一系列对象的交互,中介和者模式使得各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。

    Java设计模式之中介模式(Mediator模式)介绍

    主要介绍了Java设计模式之中介模式(Mediator模式)介绍,本文讲解了为何使用Mediator模式、如何使用中介模式等内容,需要的朋友可以参考下

    C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题示例

    本文实例讲述了C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题。分享给大家供大家参考,具体如下: 一、理论定义 中介者模式 定义了一种一对多的操作,解脱了对象之间多对多的引用依赖,所有对象之间的...

Global site tag (gtag.js) - Google Analytics