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
分享到:
Global site tag (gtag.js) - Google Analytics
|
相关推荐
C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 C#面向对象设计模式纵横谈(18):(行为型模式) Iterator 迭代器模式 C#面向对象设计模式纵横谈(19):(行为型模式) Observer 观察者模式 C#...
C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式
中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern)
中介者模式(Mediator) 用意:用一个中介对象来封装一系列对象间的交互。中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。
解释器模式将描述怎样 在 有了一个简单的文法后, 使用模式设计解释这些语句。 在解释器模式里面提到的语言是指任 何解释器对象能够解释的任何组合。在解释器模式中需要定义一个代表 文法的命令类的等 级结构,也...
C#面向对象设计模式纵横谈(17):(行为型模式) Mediator 中介者模式 (Level 300)
36种最新设计模式整理 Design Pattern: Simple Factory 模式 Design Pattern: Abstract Factory 模式 Design Pattern: Builder 模式 Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 ...
创建模式: 设计模式之Factory 设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) 结构模式: ...设计模式之Mediator(中介者) 设计模式之Interpreter(解释器) 设计模式之Visitor
中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式...
提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式(Mediator Pattern) 装饰模式...
设计模式参考文档 创建模式: 设计模式之Factory 设计模式之Prototype(原型) 设计模式之Builder 设计模式之Singleton(单态) ...设计模式之Mediator(中介者) 设计模式之Interpreter(解释器) 设计模式之Visitor
│ │ └─C#设计模式(6)——原型模式(Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.Adapter...
C#设计模式(23种设计模式) 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器...
C++设计模式课件17_Mediator_中介者.pdf
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
这是一个运用了中介者设计模式的登录对话框练习题。 该案例改编自结城浩《设计模式-java语言中的应用》一书。 This is a program about a login dialog, wich try to illustrate the design pattern of Mediator. ...
23种设计模式之十八(行为模式)Mediator模式
用一个中介者对象来封装一系列对象的交互,中介和者模式使得各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变他们之间的交互。
主要介绍了Java设计模式之中介模式(Mediator模式)介绍,本文讲解了为何使用Mediator模式、如何使用中介模式等内容,需要的朋友可以参考下
本文实例讲述了C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题。分享给大家供大家参考,具体如下: 一、理论定义 中介者模式 定义了一种一对多的操作,解脱了对象之间多对多的引用依赖,所有对象之间的...