结合项目实例 回顾传统设计模式(十一)代理模式
2011-10-07 17:58 熬夜的虫子 阅读(558) 评论(0) 编辑 收藏 举报所谓的代理,就是代表某个真实的对象。在这个设计模式中,代理可以假装自己是远程对象,但其实只是一个中间角色。客户对象所作的就像是在做远程方法调用,但其实只是调用本地资源中得“代理”对象上得方法,再由代理处理所有网络通信的底层细节。
其实其实项目实例神马的 根本就没必要了 看一下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);
}
}
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()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
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);
}
}
/// 代理
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ReceiveVSoapChannel : ProxyService.ReceiveVService.ReceiveVSoap, System.ServiceModel.IClientChannel {
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
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);
rv.Mem_handleforM(value, mc.MemoryConfig_MID);
总结:代理模式为另一个对象提供代表,以便控制客户对对象的访问,管理访问的方式有很多种。远程代理管理客户和远程对象之间的交互。
原创作品允许转载,转载时请务必以超链接形式标明文章原始出处以及作者信息。 作者:熬夜的虫子 点击查看:博文索引 |
|