23种设计模式--中介者模式-Mediator Pattern
一、中介者模式的介绍
中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再把中介和用户类分成2个类,这样就成了抽象中介者角色,具体中介者角色、抽象同事类和具体同事类。来几个例子比如说各种游戏平台,还有我们最熟悉的QQ平台,等等这些都是中介者模式的具体应用,中介者模式常用于处理通信之间复杂有关联的业务,这样就会存在一个缺点比如说因为中介者处理了好多用户之间的关系,一但发生错误影响会是多个类,理论就说到,最近大家都知道邹市明大满贯了那么我们就用打拳击这个事来说下中介者模式吧;
二、中介者模式Demo
不使用中介者模式的Demo
/// <summary>
/// 拳击者得类包括得分,和受到攻击时得分变化得方法
/// </summary>
public abstract class AbstractFight
{
//得分
public double grade;
/// <summary>
/// 受到打击时或者主动出击时得分得变化
/// </summary>
/// <param name="num"></param>
/// <param name="fight"></param>
public abstract double ChangeGrade(double num, AbstractFight fight);
}
///具体实现
public class ZouShimingFight : AbstractFight
{
/// <summary>
/// 邹市明被打时得分下降,坤比七得分上涨
/// num表示得分,fight表示坤比七对象
/// </summary>
/// <param name="num"></param>
/// <param name="fight"></param>
public override double ChangeGrade(double num, AbstractFight fight)
{
this.grade -= num;
fight.grade = fight.grade + num;
return this.grade;
}
}
public class KunBiQiFight : AbstractFight
{
public override double ChangeGrade(double num, AbstractFight fight)
{
this.grade -= num;
fight.grade = fight.grade + num;
return this.grade;
}
}
///调用
AbstractFight zouFight = new ZouShimingFight();
zouFight.grade = 100;
AbstractFight kunFigeht = new KunBiQiFight();
kunFigeht.grade = 100;
Console.WriteLine("比赛开始");
double zouGrade=zouFight.ChangeGrade(10, kunFigeht);
double kunGrade = kunFigeht.ChangeGrade(30, zouFight);
if (zouGrade > kunGrade)
{
Console.WriteLine("邹市明胜利");
}
使用中介者模式的Demo
/// <summary>
/// 拳击者得类包括得分,和受到攻击时得分变化得方法
/// </summary>
public abstract class AbstractFight
{
//得分
public double grade;
/// <summary>
/// 受到打击时或者主动出击时得分得变化
/// </summary>
/// <param name="num"></param>
/// <param name="fight"></param>
public abstract double ChangeGrade(double num, AbstractMediator fight);
}
/// <summary>
/// 抽象中介者
/// </summary>
public abstract class AbstractMediator
{
protected AbstractFight zouFight;
protected AbstractFight kunFight;
public AbstractMediator(AbstractFight _zouFight,AbstractFight _kunFight)
{
zouFight = _zouFight;
kunFight = _kunFight;
}
/// <summary>
/// 邹市明被打得分
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public abstract double ZouEmbattled(double num);
/// <summary>
/// 坤比七被打得分
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public abstract double KunEmbattled(double num);
}
/// <summary>
/// 实现抽象中介者
/// </summary>
public class MeditorFight : AbstractMediator
{
public MeditorFight(AbstractFight _zouFight, AbstractFight _kunFight) : base(_zouFight, _kunFight)
{
}
public override double KunEmbattled(double num)
{
zouFight.grade -= num;
kunFight.grade = kunFight.grade + num;
return zouFight.grade;
}
public override double ZouEmbattled(double num)
{
kunFight.grade -= num;
zouFight.grade = zouFight.grade + num;
return kunFight.grade;
}
}
/// <summary>
/// 具体实现
/// </summary>
public class ZouShimingFight : AbstractFight
{
public override double ChangeGrade(double num, AbstractMediator fight)
{
return fight.ZouEmbattled(num);
}
}
public class KunBiQiFight : AbstractFight
{
public override double ChangeGrade(double num, AbstractMediator fight)
{
return fight.KunEmbattled(num);
}
}
///调用
AbstractFight zouFight = new ZouShimingFight();
zouFight.grade = 100;
AbstractFight kunFight = new KunBiQiFight();
kunFight.grade = 100;
//解耦
AbstractMediator mFight =new MeditorFight(zouFight, kunFight);
Console.WriteLine("比赛开始");
double zouGrade=zouFight.ChangeGrade(10, mFight);
double kunGrade = kunFight.ChangeGrade(30, mFight);
if (zouGrade > kunGrade)
{
Console.WriteLine("邹市明胜利!!");
}
Console.ReadKey();
三、GitHub
https://github.com/wangtongzhou520/Designpattern