真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

設(shè)計(jì)模式-中介者模式-創(chuàng)新互聯(lián)

場(chǎng)景:服務(wù)端與客戶(hù)端通過(guò)webSocket方式通話(huà),websocket充當(dāng)中介

成都創(chuàng)新互聯(lián)是一家專(zhuān)注于成都網(wǎng)站制作、網(wǎng)站建設(shè)與策劃設(shè)計(jì),萬(wàn)山網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:萬(wàn)山等地區(qū)。萬(wàn)山做網(wǎng)站價(jià)格咨詢(xún):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 = "服務(wù)器";
        public Service(Agency _agency) : base(_agency)
        {
        }        
        public override void ReviceMsg(string sender, string msg)
        {

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

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

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

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

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

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

        //中介
        abstract class Agency
    {
        public Service service { get; set; }  //中介需要知道通話(huà)雙方

        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("零點(diǎn)停電維護(hù)");
            client1.SendMsg("我嬲!");
            client2.SendMsg("尼瑪,我PC端不接火");

            Console.Read();

        }

總結(jié):服務(wù)端與客戶(hù)端完全脫離耦合,職責(zé)相當(dāng)明確。
優(yōu)點(diǎn):減少耦合,交互的過(guò)程獨(dú)立成單獨(dú)的類(lèi)(中介類(lèi))。
缺點(diǎn):中介類(lèi)的職責(zé)過(guò)于龐大,不好維護(hù)。

中介者+職責(zé)鏈的代碼如下,純粹練手,是為了模式而模式,實(shí)際開(kāi)發(fā)可別這么做。

//TongXinObject和上面一樣

//職責(zé)···把上面中介中的if else 抽象出來(lái)
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("零點(diǎn)停電維護(hù)");
            //client1.SendMsg("我嬲!");
            //client2.SendMsg("尼瑪,我PC端不接火");

            //Console.Read();

            中介者模式.職責(zé)鏈中介.Agency agency = new 中介者模式.職責(zé)鏈中介.WebSocket();
            Service service = new Service(agency);
            Client1 client1 = new Client1(agency);
            Client2 client2 = new Client2(agency);            
            agency.SetZhiZe(service, client1, client2);

            service.SendMsg("零點(diǎn)停電維護(hù)");
            client1.SendMsg("我嬲!");
            client2.SendMsg("尼瑪,我PC端不接火");

            Console.Read();
        }
    }

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。


當(dāng)前標(biāo)題:設(shè)計(jì)模式-中介者模式-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://weahome.cn/article/jgioo.html

其他資訊

在線(xiàn)咨詢(xún)

微信咨詢(xún)

電話(huà)咨詢(xún)

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部