中介者设计模式

时间:2021-07-25 22:00:24

故事设定:房东告诉中介要出租房子。中介发布租房广告。租房者看到出租信息,告诉中介我要租这个房子。中介告诉房东有人要租房子。 租房结束。

说明:整个租房过程中,房东和租房者不直接联系的,各项活动都是通过中介进行。个人感觉:在一般的逻辑中,增加了中介者,反而会让类看起来复杂在实际的项目中,可能各个交互对象根本无法抽离出一个抽象类或者行为接口,所以不要为了抽象而去抽象。

using System;

namespace WCF_Binding
{
    class MediatorPattern
    {
        public void MainTest()
        {
            Mediator _mediator = new Mediator();

            HouseOwn _houseown = new HouseOwn();
            _houseown._mediator = _mediator;

            _mediator.houseown = _houseown;

            IRenter renter = new RichRenter();
            renter.mediator = _mediator;

            _mediator.renter = renter;

            _houseown.SendMessage();

        }
    }

    //中介发布租房信息,筛选客户
    public interface IMediator
    {
        void SendNotice(string command);
        void ReplyMessage(string reply);
    }
    public class Mediator : IMediator
    {
        static string _command = string.Empty;
        public HouseOwn houseown { get; set; }
        public IRenter renter { get; set; }

        public void SendNotice(string command)
        {
            _command = "中介发布租房通知--》有豪华房子出租";
            Console.WriteLine(_command);
            //找到潜在客户
            renter.SendMessage(_command, this);
        }
        public void ReplyMessage(string reply)
        {
            //dosomething:筛选客户,带客户看房子,客户满意后通知房主
            Console.WriteLine("中介通知房东有人要租这个房子");
            houseown.GetReply();
            Console.WriteLine("双方签订合同,租房结束");
        }
    }
    public interface IRenter
    {
        Mediator mediator { get; set; }
       void SendMessage(string notice, Mediator mediator);
    }
    //房主向中介发布租房信息
    public class HouseOwn
    {
        public Mediator _mediator { get; set; }

        private string Command
        {
            get { return "房东要出租豪华房子--》"; }//什么样的房子,房租多少钱,要求房客什么类型的
        }
        public void SendMessage()
        {
            Console.WriteLine(Command);
            _mediator.SendNotice(this.Command);
        }
        public void GetReply()
        {
            Console.WriteLine("房东知道房子有人租了");
        }
    }
    //高富帅承租人得到通知
    public class RichRenter:IRenter
    {
        Mediator _mediator;
        public Mediator mediator { get { return _mediator; } set { _mediator = value; } }
        public void SendMessage(string notice, Mediator mediator)
        {
            _mediator = mediator;
            Console.WriteLine("Renter 得到这个通告");

            DoSomething();
        }
        private void DoSomething()
        {
            Console.WriteLine("Renter--》告诉中介-我有的是钱,要租下这个房子");
            _mediator.ReplyMessage("Renter--》告诉中介-我要租下这个房子");
           
        }
    }
    public class PoorRenter : IRenter
    {
        Mediator _mediator;
        public Mediator mediator { get { return _mediator; } set { _mediator = value; } }
        public void SendMessage(string notice, Mediator mediator)
        {
            Console.WriteLine("Renter--》我没有钱,租不起价格高的房子");
        }
    }
}

结果如下:

中介者设计模式