Mediator模式
来源:互联网 发布:2017年开淘宝店铺 编辑:程序博客网 时间:2024/05/16 17:31
using System;using System.Linq;using System.Text;namespace DesignPatterns.Mediator{ //MediatorR abstract class AbstractChatroom { public abstract void Register(Participant participant); public abstract void Send(string from, string to, string message); } //ConcreteMediator class Chatroom : AbstractChatroom { private System.Collections.Hashtable participants = new System.Collections.Hashtable(); public override void Register(Participant participant) { if (participants[participant.Name] == null) { participants[participant.Name] = participant; } participant.Chatroom = this; } public override void Send(string from, string to, string message) { Participant pto = (Participant)participants[to]; if (pto != null) { pto.Receive(from, message); } } } /**********************************************************************************/ //AbstractColleague class Participant { private Chatroom chatroom; private string name; //Constructor public Participant(string name) { this.name = name; } //Properties public string Name { get { return name; } } public Chatroom Chatroom { set { chatroom = value; } get { return chatroom; } } public void Send(string to, string message) { chatroom.Send(name, to, message); } public virtual void Receive(string from, string message) { Console.WriteLine("{0} to {1}:'{2}'", from, name, message); } } //ConcreteColleaguel class Beatle : Participant { //Constructor public Beatle(string name) : base(name) { } public override void Receive(string from, string message) { Console.Write("To a Beatle: "); base.Receive(from, message); } } //ConcreteColleague2 class NonBeatle : Participant { //Constructor public NonBeatle(string name) : base(name) { } public override void Receive(string from, string message) { Console.Write("To a non-Beatle:"); base.Receive(from, message); } } class Program { //客户端调用如下: static void Main(string[] args) { //create chatroom Chatroom chatroom = new Chatroom(); //Create participants and register them Participant George = new Beatle("George"); Participant Paul = new Beatle("Paul"); Participant Ringo = new Beatle("Ringo"); Participant John = new NonBeatle("John"); Participant Yoko = new NonBeatle("Yoko"); chatroom.Register(George); chatroom.Register(Paul); chatroom.Register(Ringo); chatroom.Register(John); chatroom.Register(Yoko); //chatting participants George.Send("Yoko", "你说错话了"); Yoko.Send("George", "哪里说错了?"); //Yoko.Send("John", "Hi John"); //Paul.Send("Ringo", "All you need is love"); //Ringo.Send("George", "My sweet Lord"); //Paul.Send("John", "Can't buy me love"); //John.Send("Yoko", "My sweet love"); Console.ReadKey(); } }}
from:http://www.uml.org.cn/sjms/200906113.asp
- Mediator模式
- MEDIATOR 模式
- Mediator模式
- Mediator 模式
- Mediator 模式
- Mediator模式
- Mediator模式
- Mediator模式
- Mediator模式
- Mediator模式
- Mediator 模式
- Mediator模式
- Mediator模式
- Mediator模式
- [设计模式]Mediator模式
- 中介者(Mediator)模式
- Mediator中介者模式
- 设计模式之Mediator
- 在Ubuntu 10.04中安装 insight
- 虚函数复习
- 第六届中国Linux内核开发者大会
- DBCC CHECKDB 数据库或表修复
- 插入排序法_c实现 (3)
- Mediator模式
- 在tomcat的配置文件中写与数据库的连接源demo
- 字符串反转 算法
- 《飞鸽传书2007怎么用》这种即时通讯技术已开始普及
- 不是数学高手就免了,这道初二数学题实在太难
- Only Linux & C -- 一些看法
- ogre画圆
- Java编程思想 第8章 多态
- 【POJ 1084】 很烦人的DLX