设计模式-中介者模式-创新互联

场景:服务端与客户端通过webSocket方式通话,websocket充当中介

成都创新互联是一家专注于成都网站制作、网站建设与策划设计,万山网站建设哪家好?成都创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:万山等地区。万山做网站价格咨询:028-86922220
abstract class TongXinObject
    {        

        protected Agency agency;
        public TongXinObject(Agency _agency)
        {
            agency = _agency;
        }
        abstract public void SendMsg(string msg);
        abstract public void ReviceMsg(string sender, string msg);
    }
    class Service : TongXinObject
    {
        public string Name = "服务器";
        public Service(Agency _agency) : base(_agency)
        {
        }        
        public override void ReviceMsg(string sender, string msg)
        {

            Console.WriteLine("{0}收到{1}发送的消息:{2}", Name,sender, msg);
        }

        public override void SendMsg(string msg)
        {
            agency.TuiSongMsg(this, msg); //交给中介推送,其他的不管
        }
    }
    class Client1 : TongXinObject
    {
        public string Name = "手机APP";
        public Client1(Agency _agency) : base(_agency)
        {
        }

        public override void ReviceMsg(string sender, string msg)
        {
            Console.WriteLine("{0}收到{1}发送的消息:{2}", Name, sender, msg);
        }

        public override void SendMsg(string msg)
        {
            agency.TuiSongMsg(this, msg);            //交给中介推送
        }
    }
    class Client2 : TongXinObject
    {
        public string Name = "PC客户端";
        public Client2(Agency _agency) : base(_agency)
        {
        }

        public override void ReviceMsg(string sender, string msg)
        {
            Console.WriteLine("{0}收到{1}发送的消息:{2}", Name, sender, msg);
        }

        public override void SendMsg(string msg)
        {
            agency.TuiSongMsg(this, msg);
        }
    }

        //中介
        abstract class Agency
    {
        public Service service { get; set; }  //中介需要知道通话双方

        public Client1 client1 { get; set; }

        public Client2 client2 { get; set; }

        abstract public void TuiSongMsg(TongXinObject obj, string msg);        
    }
    class WebSocket : Agency
    {
        public override void TuiSongMsg(TongXinObject obj,string msg)
        {
            if (obj == service)
            {                               
                client1.ReviceMsg(service.Name,msg);
                client2.ReviceMsg(service.Name,msg);
            }
            else if (obj == client1)
            {                
                service.ReviceMsg(client1.Name, msg);
                client2.ReviceMsg(client1.Name, msg);
            }
            else if (obj == client2)
            {                
                service.ReviceMsg(client2.Name, msg);
                client1.ReviceMsg(client2.Name, msg);
            }
        }
    }

        //前端:
        static void Main(string[] args)
        {
            Agency agency = new WebSocket();
            Service service = new Service(agency);
            Client1 client1 = new Client1(agency);
            Client2 client2 = new Client2(agency);
            agency.service = service;
            agency.client1 = client1;
            agency.client2 = client2;

            service.SendMsg("零点停电维护");
            client1.SendMsg("我嬲!");
            client2.SendMsg("尼玛,我PC端不接火");

            Console.Read();

        }

总结:服务端与客户端完全脱离耦合,职责相当明确。
优点:减少耦合,交互的过程独立成单独的类(中介类)。
缺点:中介类的职责过于庞大,不好维护。

中介者+职责链的代码如下,纯粹练手,是为了模式而模式,实际开发可别这么做。

//TongXinObject和上面一样

//职责···把上面中介中的if else 抽象出来
abstract class ZhiZe
    {
        public Service service { get; set; }

        public Client1 client1 { get; set; }

        public Client2 client2 { get; set; }
        public ZhiZe(Service _service, Client1 _client1, Client2 _client2)
        {
            service = _service;
            client1 = _client1;
            client2 = _client2;
        }
        public ZhiZe superZhiZe { get; set; }
        public void SetSuperZhiZe(ZhiZe _zhiZe)
        {
            superZhiZe = _zhiZe;
        }
        abstract public void TuiSongMsg(TongXinObject tongXinObject, string msg);
    }
    class Client1ZhiZe : ZhiZe
    {
        public Client1ZhiZe(Service _service, Client1 _client1, Client2 _client2) : base(_service, _client1, _client2)
        {
        }

        public override void TuiSongMsg(TongXinObject tongXinObject, string msg)
        {
            if (tongXinObject == client1)
            {
                service.ReviceMsg(client1.Name, msg);
                client2.ReviceMsg(client1.Name, msg);
            }
            else
            {
                superZhiZe.TuiSongMsg(tongXinObject, msg);
            }
        }
    }
    class Client2ZhiZe : ZhiZe
    {
        public Client2ZhiZe(Service _service, Client1 _client1, Client2 _client2) : base(_service, _client1, _client2)
        {
        }

        public override void TuiSongMsg(TongXinObject tongXinObject, string msg)
        {
            if (tongXinObject == client2)
            {
                service.ReviceMsg(client2.Name, msg);
                client1.ReviceMsg(client2.Name, msg);
            }
            else
            {
                superZhiZe.TuiSongMsg(tongXinObject, msg);
            }
        }
    }
    class ServiceZhiZe : ZhiZe
    {
        public ServiceZhiZe(Service _service, Client1 _client1, Client2 _client2) : base(_service, _client1, _client2)
        {
        }

        public override void TuiSongMsg(TongXinObject tongXinObject, string msg)
        {
            if (tongXinObject == service)
            {
                client1.ReviceMsg(service.Name, msg);
                client2.ReviceMsg(service.Name, msg);
            }
            else
            {
                Console.WriteLine("over");
            }
        }
    }

//中介
abstract class Agency
    {       
        public ZhiZe ZhiZe1 { get; set; }

        public ZhiZe ZhiZe2 { get; set; }

        public ZhiZe ZhiZe3 { get; set; }

        public Agency()
        {

        }
        public void SetZhiZe(Service service, Client1 client1, Client2 client2)
        {
            ZhiZe1 = new Client1ZhiZe(service, client1, client2);
            ZhiZe2 = new Client2ZhiZe(service, client1, client2);
            ZhiZe3 = new ServiceZhiZe(service, client1, client2);

            ZhiZe1.SetSuperZhiZe(ZhiZe2);
            ZhiZe2.SetSuperZhiZe(ZhiZe3);
        }
        abstract public void TuiSongMsg(TongXinObject obj, string msg);
    }
    class WebSocket : Agency
    {
        public override void TuiSongMsg(TongXinObject obj, string msg)
        {
            ZhiZe1.TuiSongMsg(obj, msg);
        }
    }

    //前端:
    static void Main(string[] args)
        {
            //Agency agency = new WebSocket();
            //Service service = new Service(agency);
            //Client1 client1 = new Client1(agency);
            //Client2 client2 = new Client2(agency);
            //agency.service = service;
            //agency.client1 = client1;
            //agency.client2 = client2;

            //service.SendMsg("零点停电维护");
            //client1.SendMsg("我嬲!");
            //client2.SendMsg("尼玛,我PC端不接火");

            //Console.Read();

            中介者模式.职责链中介.Agency agency = new 中介者模式.职责链中介.WebSocket();
            Service service = new Service(agency);
            Client1 client1 = new Client1(agency);
            Client2 client2 = new Client2(agency);            
            agency.SetZhiZe(service, client1, client2);

            service.SendMsg("零点停电维护");
            client1.SendMsg("我嬲!");
            client2.SendMsg("尼玛,我PC端不接火");

            Console.Read();
        }
    }

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:设计模式-中介者模式-创新互联
分享链接:http://ybzwz.com/article/jgioo.html