天天看點

結合項目執行個體 回顧傳統設計模式(十一)代理模式

所謂的代理,就是代表某個真實的對象。在這個設計模式中,代理可以假裝自己是遠端對象,但其實隻是一個中間角色。客戶對象所作的就像是在做遠端方法調用,但其實隻是調用本地資源中得“代理”對象上得方法,再由代理處理所有網絡通信的底層細節。

其實其實項目執行個體神馬的 根本就沒必要了 看一下Web Service的調用方式大家也許就明白了,它會在用戶端生成一個代理類 - - 已經很完美的诠釋了代理模式這個概念

蟲子放下水 直接拿以前監控項目中用戶端采集的代理方法了 --_____--

伺服器端

        [WebMethod]

        public void Mem_handleforM(string value,int monitorid)

        {

            try

            {

                string IpInfo = WebUtils.GetClientIP();

                View_Mem vm = new View_Mem();

                vm.Ip = IpInfo;

                vm.MonitorTime = DateTime.Now;

                vm.Value = value;

                vm.monitorid = monitorid;

                //ViewService vs = new ViewService();

                ViewService.Instance.AddMemValue(vm);

            }

            catch (Exception ex)

                ExceptionHandler.Instance.HandleException(ex);

        }

代理

/// <summary>

    /// 代理

    /// </summary>

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]

    public interface ReceiveVSoapChannel : ProxyService.ReceiveVService.ReceiveVSoap, System.ServiceModel.IClientChannel {

    }

    [System.Diagnostics.DebuggerStepThroughAttribute()]

    public partial class ReceiveVSoapClient : System.ServiceModel.ClientBase<ProxyService.ReceiveVService.ReceiveVSoap>, ProxyService.ReceiveVService.ReceiveVSoap

    {

        public ReceiveVSoapClient()

        [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]

        ProxyService.ReceiveVService.Mem_handleforMResponse ProxyService.ReceiveVService.ReceiveVSoap.Mem_handleforM(ProxyService.ReceiveVService.Mem_handleforMRequest request)

            return base.Channel.Mem_handleforM(request);

        public void Mem_handleforM(string value, int monitorid)

            ProxyService.ReceiveVService.Mem_handleforMRequest inValue = new ProxyService.ReceiveVService.Mem_handleforMRequest();

            inValue.Body = new ProxyService.ReceiveVService.Mem_handleforMRequestBody();

            inValue.Body.value = value;

            inValue.Body.monitorid = monitorid;

            ProxyService.ReceiveVService.Mem_handleforMResponse retVal = ((ProxyService.ReceiveVService.ReceiveVSoap)(this)).Mem_handleforM(inValue);

用戶端

  ProxyService.ReceiveVService.ReceiveVSoapClient rv = new ReceiveVSoapClient();

                    rv.Mem_handleforM(value, mc.MemoryConfig_MID);

總結:代理模式為另一個對象提供代表,以便控制客戶對對象的通路,管理通路的方式有很多種。遠端代理管理客戶和遠端對象之間的互動。

本文轉自 熬夜的蟲子  51CTO部落格,原文連結:http://blog.51cto.com/dubing/712418

下一篇: RMAN介紹

繼續閱讀