1:概述
用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。简单点来说,将原来两个直接引用或者依赖的对象拆开,在中间加入一个“中介”对象,使得两头的对象分别和“中介”对象引用或者依赖
2:类图
1) 抽象中介者(Mediator)角色:抽象中介者角色定义统一的接口用于各同事角色之间的通信。
2) 具体中介者(Concrete Mediator)角色:具体中介者角色通过协调各同事角色实现协作行为。为此它要知道并引用各个同事角色。
3) 同事(Colleague)角色:每一个同事角色都知道对应的具体中介者角色,而且与其他的同事角色通信的时候,一定要通过中介者角色协作
3:代码
/// <summary>
/// 抽象中介者(Mediator)角色
/// </summary>
public class Mediator
{
Boy boy;
Girl girl;
public void RegistBoy(Boy boy)
{
this.boy = boy;
}
public void RegistGirl(Girl girl)
{
this.girl = girl;
}
public bool FindPartner(Person person)
{
if (person.GetType().ToString() == "Bll.Mediator.Boy")
{
this.boy = (Boy)person;
}
else
{
this.girl = (Girl)person;
}
bool isPartner = this.boy.getCondition() == this.girl.getCondition();
System.Console.Write(this.boy.getName() + "与" + this.girl.getName() + "是情侣.");
return isPartner;
}
}
public abstract class Person
{
//姓名
string name = string.Empty;
//条件
int condition;
//中介者(不管男孩还是女孩,都需要知道中介者,通过中介者去找对象)
public Mediator mediator;
public Person(Mediator mediator, String name, int condition)
{
this.mediator = mediator;
this.name = name;
this.condition = condition;
}
public abstract bool FindPartner(Person person);
public int getCondition()
{
return condition;
}
public void setCondition(int condition)
{
this.condition = condition;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
/// <summary>
/// 同事(Colleague)角色
/// </summary>
public class Boy : Person
{
public Boy(Mediator mediator, String name, int condition)
: base(mediator, name, condition)
{
//注册自己
mediator.RegistBoy(this);
}
public override bool FindPartner(Person person)
{
System.Console.Write("Boy try to find girl");
return mediator.FindPartner(person);
}
}
/// <summary>
/// 同事(Colleague)角色
/// </summary>
public class Girl : Person
{
public Girl(Mediator mediator, String name, int condition)
: base(mediator, name, condition)
{
//注册自己
mediator.RegistGirl(this);
}
public override bool FindPartner(Person person)
{
System.Console.Write("Girl try to find boy");
return mediator.FindPartner(person);
}
}
//调用
Bll.Mediator.Mediator mediator = new Bll.Mediator.Mediator();
//一个名为Zhangshan的男孩,条件为80
Bll.Mediator.Boy zhangshan = new Bll.Mediator.Boy(mediator, "Zhangshan", 80);
Bll.Mediator.Girl lili = new Bll.Mediator.Girl(mediator, "Lili", 80);
//如果条件符合,则partner成立
zhangshan.FindPartner(lili);
//一个名为lanlan的女孩,条件为90
Bll.Mediator.Girl lanlan = new Bll.Mediator.Girl(mediator, "lanlan", 90);
lanlan.FindPartner(zhangshan);
/// 抽象中介者(Mediator)角色
/// </summary>
public class Mediator
{
Boy boy;
Girl girl;
public void RegistBoy(Boy boy)
{
this.boy = boy;
}
public void RegistGirl(Girl girl)
{
this.girl = girl;
}
public bool FindPartner(Person person)
{
if (person.GetType().ToString() == "Bll.Mediator.Boy")
{
this.boy = (Boy)person;
}
else
{
this.girl = (Girl)person;
}
bool isPartner = this.boy.getCondition() == this.girl.getCondition();
System.Console.Write(this.boy.getName() + "与" + this.girl.getName() + "是情侣.");
return isPartner;
}
}
public abstract class Person
{
//姓名
string name = string.Empty;
//条件
int condition;
//中介者(不管男孩还是女孩,都需要知道中介者,通过中介者去找对象)
public Mediator mediator;
public Person(Mediator mediator, String name, int condition)
{
this.mediator = mediator;
this.name = name;
this.condition = condition;
}
public abstract bool FindPartner(Person person);
public int getCondition()
{
return condition;
}
public void setCondition(int condition)
{
this.condition = condition;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
/// <summary>
/// 同事(Colleague)角色
/// </summary>
public class Boy : Person
{
public Boy(Mediator mediator, String name, int condition)
: base(mediator, name, condition)
{
//注册自己
mediator.RegistBoy(this);
}
public override bool FindPartner(Person person)
{
System.Console.Write("Boy try to find girl");
return mediator.FindPartner(person);
}
}
/// <summary>
/// 同事(Colleague)角色
/// </summary>
public class Girl : Person
{
public Girl(Mediator mediator, String name, int condition)
: base(mediator, name, condition)
{
//注册自己
mediator.RegistGirl(this);
}
public override bool FindPartner(Person person)
{
System.Console.Write("Girl try to find boy");
return mediator.FindPartner(person);
}
}
//调用
Bll.Mediator.Mediator mediator = new Bll.Mediator.Mediator();
//一个名为Zhangshan的男孩,条件为80
Bll.Mediator.Boy zhangshan = new Bll.Mediator.Boy(mediator, "Zhangshan", 80);
Bll.Mediator.Girl lili = new Bll.Mediator.Girl(mediator, "Lili", 80);
//如果条件符合,则partner成立
zhangshan.FindPartner(lili);
//一个名为lanlan的女孩,条件为90
Bll.Mediator.Girl lanlan = new Bll.Mediator.Girl(mediator, "lanlan", 90);
lanlan.FindPartner(zhangshan);