代理模式(Proxy)

1.    定义

       为其他对象提供一种代理以控制对这个对象的访问。

2.      UML 类图

 

3.      结构代码

// Proxy pattern -- Structural example

using System;

 

namespace DoFactory.GangOfFour.Proxy.Structural

{

  /// <summary>

  /// MainApp startup class for Structural

  /// Proxy Design Pattern.

  /// </summary>

  class MainApp

  {

    /// <summary>

    /// Entry point into console application.

    /// </summary>

    static void Main()

    {

      // Create proxy and request a service

      Proxy proxy = new Proxy();

      proxy.Request();

 

      // Wait for user

      Console.ReadKey();

    }

  }

 

  /// <summary>

  /// The 'Subject' abstract class

  /// </summary>

  abstract class Subject

  {

    public abstract void Request();

  }

 

  /// <summary>

  /// The 'RealSubject' class

  /// </summary>

  class RealSubject : Subject

  {

    public override void Request()

    {

      Console.WriteLine("Called RealSubject.Request()");

    }

  }

 

  /// <summary>

  /// The 'Proxy' class

  /// </summary>

  class Proxy : Subject

  {

    private RealSubject _realSubject;

 

    public override void Request()

    {

      // Use 'lazy initialization'

      if (_realSubject == null)

      {

        _realSubject = new RealSubject();

      }

 

      _realSubject.Request();

    }

  }

}


Output
Called RealSubject.Request()

4.      实例代码

// Proxy pattern -- Real World example

using System;

 

namespace DoFactory.GangOfFour.Proxy.RealWorld

{

  /// <summary>

  /// MainApp startup class for Real-World

  /// Proxy Design Pattern.

  /// </summary>

  class MainApp

  {

    /// <summary>

    /// Entry point into console application.

    /// </summary>

    static void Main()

    {

      // Create math proxy

      MathProxy proxy = new MathProxy();

 

      // Do the math

      Console.WriteLine("4 + 2 = " + proxy.Add(4, 2));

      Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2));

      Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2));

      Console.WriteLine("4 / 2 = " + proxy.Div(4, 2));

 

      // Wait for user

      Console.ReadKey();

    }

  }

 

  /// <summary>

  /// The 'Subject interface

  /// </summary>

  public interface IMath

  {

    double Add(double x, double y);

    double Sub(double x, double y);

    double Mul(double x, double y);

    double Div(double x, double y);

  }

 

  /// <summary>

  /// The 'RealSubject' class

  /// </summary>

  class Math : IMath

  {

    public double Add(double x, double y) { return x + y; }

    public double Sub(double x, double y) { return x - y; }

    public double Mul(double x, double y) { return x * y; }

    public double Div(double x, double y) { return x / y; }

  }

 

  /// <summary>

  /// The 'Proxy Object' class

  /// </summary>

  class MathProxy : IMath

  {

    private Math _math = new Math();

 

    public double Add(double x, double y)

    {

      return _math.Add(x, y);

    }

    public double Sub(double x, double y)

    {

      return _math.Sub(x, y);

    }

    public double Mul(double x, double y)

    {

      return _math.Mul(x, y);

    }

    public double Div(double x, double y)

    {

      return _math.Div(x, y);

    }

  }

}


Output
4 + 2 = 6
4 - 2 = 2
4 * 2 = 8
4 / 2 = 2

该文章来自:http://www.dofactory.com/Patterns/PatternProxy.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值