浅尝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



    原文地址:https://www.cnblogs.com/TivonStone/p/1719294.html