您的位置:首页 > 编程语言 > C#

中介者模式 C#

2016-01-11 15:01 573 查看
  相亲的时候如果有个媒婆,那么就会避免很多不必要的麻烦了撒。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
class Program
{

public abstract class Person
{
public string Name
{
get;
set;
}

public string Condition
{
get;
set;
}

public abstract void GetPartner(Person p);
}

public class Mediator
{
public Women Women
{
get;
set;
}

public Man Man
{
get;
set;
}

public void GetPartner(Person w, Person m)
{
if (w.GetType() == m.GetType())
{
Console.WriteLine("same sex");
}
else if (w.Condition == m.Condition)
{
Console.WriteLine(w.Name + " and " + m.Name + " match");
}
else
{
Console.WriteLine(w.Name + " and " + m.Name + " not match");
}
}
}

public class Man:Person
{
public Mediator Mediator
{
get;
set;
}

public Man(string name, string condition, Mediator m)
{
this.Name = name;
this.Condition = condition;
this.Mediator = m;
Mediator.Man = this;
}

public override void GetPartner(Person p)
{
this.Mediator.GetPartner(this, p);
}
}

public class Women : Person
{
public Mediator Mediator
{
get;
set;
}

public Women(string name, string condition, Mediator m)
{
this.Name = name;
this.Condition = condition;
this.Mediator = m;
m.Women = this;
}

public override void GetPartner(Person p)
{
this.Mediator.GetPartner(this, p);
}
}

static void Main(string[] args)
{
Mediator m = new Mediator();
Person zk = new Man("zk", "5", m);
Person yzl = new Women("yzl", "5", m);

zk.GetPartner(yzl);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: