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

这里我们做一个简单的设计模式的组合,中介者模式+代理模式。代码如下:

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的开发经验,我想找一份与之相关的工作!");
            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值