中介者模式

Posted LoveTomato

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了中介者模式相关的知识,希望对你有一定的参考价值。

模式定义

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

UML类图

  • 抽象中介者 Mediator 定义一个接口,该接口用于与各同事对象之间的通信方法Send(stirng msg)
  • 具体中介者 ConcreteMediator 抽象中介者的子类,依赖同事类,实现抽象中介者接口()。
  • 抽象同事类 Colleague 关联抽象中介对象,定义各同事的抽象方法(包发送同事方法Send(string msg)和接受同事方法Notify(string msg))。
  • 具体同事类 ConcreteColleague 同事类的子类,实现同事间发送消息和接受消息

代码结构

	public static class MediatorApp
	{
		public static void Run()
		{
			ConcreteMediator m = new ConcreteMediator();

			ConcreteColleague1 c1 = new ConcreteColleague1(m);
			ConcreteColleague2 c2 = new ConcreteColleague2(m);

			m.Colleague1 = c1;
			m.Colleague2 = c2;

			c1.Send("How are you?");
			c2.Send("Fine,thanks");

			Console.ReadKey();
		}
	}
	/// <summary>
	/// 抽象中介者
	/// </summary>
	abstract class Mediator
	{
		public abstract void Send(string message, Colleague colleague);
	}

	/// <summary>
	/// 具体中介者(管理相互交换的两个‘同事’类)
	/// </summary>
	class ConcreteMediator : Mediator
	{
		private ConcreteColleague1 _colleague1;
		private ConcreteColleague2 _colleague2;

		public ConcreteColleague1 Colleague1
		{
			set { _colleague1 = value; }
		}

		public ConcreteColleague2 Colleague2
		{
			set { _colleague2 = value; }
		}

		public override void Send(string message, Colleague colleague)
		{
			if(colleague == _colleague1)
			{
				_colleague2.Notify(message);
			}
			else
			{
				_colleague1.Notify(message);
			}
		}
	}

	/// <summary>
	/// 抽象同事类,关联抽象中介者,两个“同事”的通信通过中介者
	/// </summary>
	abstract class Colleague
	{
		protected Mediator mediator;

		public Colleague(Mediator mediator)
		{
			this.mediator = mediator;
		}
	}

	/// <summary>
	/// 具体中介者
	/// </summary>
	class ConcreteColleague1 : Colleague
	{
		public ConcreteColleague1(Mediator mediator) : base(mediator)
		{
		}

		public void Send(string message)
		{
			mediator.Send(message, this);
		}

		public void Notify(string message)
		{
			Console.WriteLine("Colleague1 gets message:"+message);
		}
	}
	/// <summary>
	/// 具体中介者
	/// </summary>
	class ConcreteColleague2 : Colleague
	{
		public ConcreteColleague2(Mediator mediator) : base(mediator)
		{
		}

		public void Send(string message)
		{
			mediator.Send(message, this);
		}
		public void Notify(string message)
		{
			Console.WriteLine("Colleague2 gets message:" + message);
		}
	}

情景案例

这里实现个简单的小游戏,游戏的参与者有CumputerPerson两个角色,参与者可以攻击不同角色,受到攻击,死亡后通知所有参与者,接受通知。

public static class RealWorldMediatorApp
	{
		public static void Run()
		{
			Game game = new Game();

			Computer com1 = new Computer(game, "Computer1");
			Computer com2 = new Computer(game, "Computer2");

			Person per1 = new Person(game, "Person1");
			Person per2 = new Person(game, "Person2");

			game.Register(com1);
			game.Register(com2);
			game.Register(per1);
			game.Register(per2);


			for (int i = 0; i < 10; i++)
			{
				com1.Attack("Person1");
				per2.Attack("Computer2");
				com2.Attack("Person2");
				per1.Attack("Computer1");

			}

			Console.ReadKey();
		}
	}
	/// <summary>
	/// 抽象中介者
	/// </summary>
	abstract class AbstractGame
	{
		public abstract void Register(Member member);
		public abstract void Attack(Member attacker, string attackee);
		public abstract void Notify(Member member);
	}

	/// <summary>
	/// 具体中介者(管理相互交换的两个‘同事’类)
	/// </summary>
	class Game : AbstractGame
	{

		private Dictionary<string, Member> _computerLst = new Dictionary<string, Member>();
		private Dictionary<string, Member> _personLst = new Dictionary<string, Member>();

		public override void Attack(Member attacker, string attackee)
		{
			Member attackeeMember;
			if (attacker.GetType() == typeof(Computer))
			{
				attackeeMember = _personLst[attackee];
			}
			else
			{
				attackeeMember = _computerLst[attackee];
			}
			attackeeMember.Hurt();
		}

		public override void Notify(Member member)
		{
			foreach (var item in _personLst)
			{
				if (item.Value.IsLive)
				{
					string msg = member.Name + " is dead";
					item.Value.ReceiveNotify(msg);
				}
			}

			foreach (var item in _computerLst)
			{
				if (item.Value.IsLive)
				{
					string msg = member.Name + " is dead";
					item.Value.ReceiveNotify(msg);
				}
			}
		}

		public override void Register(Member member)
		{
			if (member.GetType() == typeof(Computer))
			{
				_computerLst.Add(member.Name, member);
			}
			else
			{
				_personLst.Add(member.Name, member);
			}
		}
	}

	/// <summary>
	/// 抽象同事类,关联抽象中介者,两个“同事”的通信通过中介者
	/// </summary>
	abstract class Member
	{
		public bool IsLive { get; private set; }

		private string _name = string.Empty;

		public string Name
		{
			get
			{
				return _name;
			}
		}

		protected AbstractGame mediator;

		public Member(AbstractGame mediator, string name)
		{
			this.mediator = mediator;
			this._name = name;
			this.IsLive = true;
		}

		public void Attack(string attackee)
		{
			if (this.IsLive)
			{
				mediator.Attack(this, attackee);
			}
		}

		public void Hurt()
		{
			Random rd = new Random();
			int ranNum = rd.Next(0, 2);
			if (this.IsLive && ranNum == 1)
			{
				this.IsLive = false;
				mediator.Notify(this);
			}
		}

		public void ReceiveNotify(string msg)
		{
			string strMsg = this.Name + " Receive :" + msg;
			Console.WriteLine(strMsg);
		}
	}

	/// <summary>
	/// 电脑角色
	/// </summary>
	class Computer : Member
	{
		public Computer(AbstractGame mediator, string name) : base(mediator, name)
		{
		}
	}
	/// <summary>
	/// 人员角色
	/// </summary>
	class Person : Member
	{
		public Person(AbstractGame mediator, string name) : base(mediator, name)
		{
		}
	}

以上是关于中介者模式的主要内容,如果未能解决你的问题,请参考以下文章

(十九)中介者模式-代码实现

设计模式之中介者模式

20160227.CCPP体系详解(0037天)

设计模式----中介者模式及简单总结(2018/10/30)

中介者模式分析结构图及基本代码

设计模式之中介者模式