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

C#设计模式扩展——Meditor+Proxy模式实现

2016-12-05 11:10 567 查看
这里我们做一个简单的设计模式的组合,中介者模式+代理模式。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
public class Agent:AbstractNeeder
{
public Agent(AbstractMeditor meditor)
: base(meditor)
{

}
private Needer need;
public Needer Need
{
get { return need; }
set { need = value; }
}
public void Sending(string message)
{
meditor.Send(message,need);
}
public void Notify(string message)
{
Console.WriteLine("待业者收到信息:" + message);
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
public class Meditor:AbstractMeditor
{
private employeer employeering;

public employeer Employeering
{
get { return employeering; }
set { employeering = value; }
}

private Agent agent;

public Agent Agent
{
get { return agent; }
set { agent = value; }
}

public override void Send(string Message, AbstractNeeder employeer)
{
if (employeer == employeering)
{
employeering.Sending(Message);
}
else
{
agent.Sending(Message);
}
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
public class Needer:AbstractNeeder
{
public Needer(AbstractMeditor meditor)
: base(meditor)
{ }
public void Sending(string message)
{
meditor.Send(message,this);
}
public void Notify(string message )
{
Console.WriteLine("待业者收到信息:"+message);
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
public class employeer:AbstractNeeder
{
public employeer(AbstractMeditor meditor)
: base(meditor)
{ }
public void Sending(string message)
{
meditor.Send(message,this );
}
public void Notify(string message )
{
Console.WriteLine("企业收到信息:"+message);
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
public abstract class AbstractNeeder
{
protected AbstractMeditor meditor;
public AbstractNeeder(AbstractMeditor meditor)
{
this.meditor = meditor;
}

}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
public abstract class AbstractMeditor
{
public abstract void Send(string Message, AbstractNeeder employeer);
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
class Program
{
static void Main(string[] args)
{
Meditor md = new Meditor();
Needer nd = new Needer(md);
employeer ep = new employeer(md);
Agent ag = new Agent(md);

ag.Notify("我需要一个熟练使用unity3D的开发!");
ep.Notify("我有Unity3D的开发经验,我想找一份与之相关
a1cf
的工作!");
Console.ReadKey();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐