zoukankan      html  css  js  c++  java
  • 浅尝DesignPattern_Proxy

    UML:

  • Proxy   (MathProxy)
    • maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.
    • 维持一个引用让代理访问真正的Subject。代理可以引用一个Subject,如果RealSubject和Subject接口是相同的。
    • provides an interface identical to Subject's so that a proxy can be substituted for for the real subject.
    • 提供等同于Subject 的接口,这样代理就能替代真正的Subject 
    • controls access to the real subject and may be responsible for creating and deleting it.
    • 控制进入真正的Subject 而且负责创建和删除它
    • other responsibilites depend on the kind of proxy:其他责任取决于代理的类型
      • remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.
      • 远程代理是负责编码空间的要求,其参数和真正的问题在一个不同的地址编码发送请求。
      • virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images's extent.
      • 虚拟代理可缓存的其他信息的真正Subject ,使他们可以推迟访问它。例如,ImageProxy的用处就是缓存实际图像的。
      • protection proxies check that the caller has the access permissions required to perform a request.
      • 保护代理检查调用方拥有访问权限的规定要求执行。
  • Subject   (IMath)
    • defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.
    • 定义RealSubject和代理的共同界面,使代理可以在任何地方使用RealSubject。
  • RealSubject   (Math)
    • defines the real object that the proxy represents. 
    • 定义了真正的对象,是代理代表的对象。

    Sample:

    IMath.cs

    1 public interface IMath
    2 {
    3 double Add(double x, double y);
    4 double Sub(double x, double y);
    5 double Mul(double x, double y);
    6 double Div(double x, double y);
    7 }

    Math.cs

    1 class Math:IMath
    2 {
    3 #region IMath 成员
    4
    5 public double Add(double x, double y)
    6 {
    7 return x + y;
    8 }
    9
    10 public double Sub(double x, double y)
    11 {
    12 return x - y;
    13 }
    14
    15 public double Mul(double x, double y)
    16 {
    17 return x * y;
    18 }
    19
    20 public double Div(double x, double y)
    21 {
    22 return x / y;
    23 }
    24
    25 #endregion
    26 }

    MathProxy.cs
    1 class MathProxy:IMath
    2 {
    3 private Math math = new Math();
    4 #region IMath 成员
    5
    6 public double Add(double x, double y)
    7 {
    8 return math.Add(x, y);
    9 }
    10
    11 public double Sub(double x, double y)
    12 {
    13 return math.Sub(x, y);
    14 }
    15
    16 public double Mul(double x, double y)
    17 {
    18 return math.Mul(x, y);
    19 }
    20
    21 public double Div(double x, double y)
    22 {
    23 return math.Div(x, y);
    24 }
    25
    26 #endregion
    27 }
    Program.cs
    1 #region Proxy
    2 MathProxy proxy = new MathProxy();
    3 Console.WriteLine("4 + 2 = " + proxy.Add(4, 2));
    4 Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2));
    5 Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2));
    6 Console.WriteLine("4 / 2 = " + proxy.Div(4, 2));
    7 #endregion



查看全文
  • 相关阅读:
    微博CacheService架构浅析 对底层协议进行适配
    Lucene 查询原理 传统二级索引方案 倒排链合并 倒排索引 跳表 位图
    Linux kernel 同步机制
    对话 CTO〡用声音在一起,听荔枝 CTO 丁宁聊 UGC 声音互动平台的技术世界 原创 王颖奇 极客公园 2018-12-01
    当中台遇上DDD,我们该如何设计微服务?
    京东技术沙龙系列之二 | 深度解析京东微服务组件平台
    gRPC设计动机和原则
    微信全文搜索优化之路
    门户级UGC系统的技术进化路线——新浪新闻评论系统的架构演进和经验总结 提高响应性能的手段归根结底就是三板斧:队列(Queue)、缓存(Cache)和分区(Sharding)
    现加减乘除4则运算
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1719294.html
  • Copyright © 2011-2022 走看看