经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式-代理模式
来源:cnblogs  作者:冰乐  时间:2019/9/20 9:49:33  对本文有异议

通过代理完成对业务类的访问,包一层方便人以功能扩展。

代理模式:VPN代理,翻墙代理,火车票代理等。

通过代理业务类去完成对真实业务类的调用,代理类不能扩展业务功能,在不修改RealSubject前提下,插入功能。

包一层:没有什么技术问题是包一层解决不了的,如果有,那么就再包一层。比如来个日志记录,可以避免修改业务类,只需要修改代理类。在来个异常处理;在来个性能提成----缓存结果........

通过代理,能够为对象扩展功能(不是增加业务)而不去修改原始业务类,也就是包了一层,我的地盘听我的

  1. /// <summary>
  2. /// 业务接口
  3. /// </summary>
  4. public interface ISubject
  5. {
  6. /// <summary>
  7. /// get
  8. /// </summary>
  9. /// <returns></returns>
  10. bool GetSomething();
  11. /// <summary>
  12. /// do
  13. /// </summary>
  14. void DoSomething();
  15. }
  1. public class ProxySubject : ISubject
  2. {
  3. //组合一下
  4. private static ISubject _Subject = new RealSubject();
  5. public void DoSomething()
  6. {
  7. try
  8. {
  9. Console.WriteLine("prepare DoSomething...");
  10. _Subject.DoSomething();
  11. }
  12. catch (Exception ex)
  13. {
  14. Console.WriteLine(ex.Message);
  15. throw ex;
  16. }
  17. }
  18. private static Dictionary<string, bool> ProxyDictionary = new Dictionary<string, bool>();
  19. public bool GetSomething()
  20. {
  21. try
  22. {
  23. Console.WriteLine("prepare GetSomething...");
  24. string key = "Proxy_GetSomething";
  25. bool bResult = false;
  26. if (!ProxyDictionary.ContainsKey(key))
  27. {
  28. bResult = _Subject.GetSomething();
  29. ProxyDictionary.Add(key, bResult);
  30. }
  31. else
  32. {
  33. bResult = ProxyDictionary[key];
  34. }
  35. return bResult;
  36. }
  37. catch (Exception ex)
  38. {
  39. Console.WriteLine(ex.Message);
  40. throw ex;
  41. }
  42. }
  43. }
View Code
  1. /// <summary>
  2. /// 一个耗时耗资源的对象方法
  3. /// 一个第三方封装的类和方法
  4. /// </summary>
  5. public class RealSubject : ISubject
  6. {
  7. public RealSubject()
  8. {
  9. Thread.Sleep(2000);
  10. long lResult = 0;
  11. for (int i = 0; i < 100000000; i++)
  12. {
  13. lResult += i;
  14. }
  15. Console.WriteLine("RealSubject被构造。。。");
  16. }
  17. /// <summary>
  18. /// 火车站查询火车票
  19. /// </summary>
  20. public bool GetSomething()
  21. {
  22. Console.WriteLine("坐车去火车站看看余票信息。。。");
  23. Thread.Sleep(3000);
  24. Console.WriteLine("到火车站,看到是有票的");
  25. return true;
  26. }
  27. /// <summary>
  28. /// 火车站买票
  29. /// </summary>
  30. public void DoSomething()
  31. {
  32. Console.WriteLine("开始排队。。。");
  33. Thread.Sleep(2000);
  34. Console.WriteLine("终于买到票了。。。");
  35. }
  36. }
View Code

调用一下:

  1. {
  2. Console.WriteLine("***********Real**************");
  3. ISubject subject = new RealSubject();
  4. subject.GetSomething();
  5. //subject.DoSomething();
  6. }
  7. {
  8. Console.WriteLine("***********Proxy**************");
  9. ISubject subject = new ProxySubject();
  10. subject.GetSomething();
  11. //subject.DoSomething();
  12. }
  13. {
  14. Console.WriteLine("***********Proxy**************");
  15. ISubject subject = new ProxySubject();
  16. subject.GetSomething();
  17. //subject.DoSomething();
  18. }
View Code

调用下看下结果:

 

 

 通过代理,能够为对象扩展功能(不是增加业务),而不是去修改原始业务类,就是包一层。

WebService就是代理模式。

 

 

 

原文链接:http://www.cnblogs.com/taotaozhuanyong/p/11552254.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号