您的位置:首页 > 编程语言 > C#

Design Pattern - Proxy(C#)

2010-12-02 11:10 232 查看

Definition

Provide a surrogate or placeholder for another object to control access to it.

Participants

    The classes and/or objects participating in this pattern are:

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.
Provides an interface identical to Subject's so that a proxy can be substituted for the real subject.
Controls access to the real subject and may be responsible for creating and deleting it.
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.
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  (Math)
Defines the real object that the proxy represents.

Sample Code in C#

This
structural code demonstrates the Proxy pattern which provides a representative object (proxy) that controls access to another similar object.

// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chimomo's Company" file="Program.cs">
// Respect the work.
// </copyright>
// <summary>
// Structural Proxy Design Pattern.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CSharpLearning
{
using System;

/// <summary>
/// Startup class for Structural Proxy Design Pattern.
/// </summary>
internal static class Program
{
#region Methods

/// <summary>
/// Entry point into console application.
/// </summary>
private static void Main()
{
// Create proxy and request a service
var proxy = new Proxy();
proxy.Request();
}

#endregion
}

/// <summary>
/// The 'Subject' abstract class
/// </summary>
internal abstract class Subject
{
#region Public Methods and Operators

/// <summary>
/// The request.
/// </summary>
public abstract void Request();

#endregion
}

/// <summary>
/// The 'RealSubject' class
/// </summary>
internal class RealSubject : Subject
{
#region Public Methods and Operators

/// <summary>
/// The request.
/// </summary>
public override void Request()
{
Console.WriteLine("Called RealSubject.Request()");
}

#endregion
}

/// <summary>
/// The 'Proxy' class
/// </summary>
internal class Proxy : Subject
{
#region Fields

/// <summary>
/// The real subject.
/// </summary>
private RealSubject realSubject;

#endregion

#region Public Methods and Operators

/// <summary>
/// The request.
/// </summary>
public override void Request()
{
// Use 'lazy initialization'
if (this.realSubject == null)
{
this.realSubject = new RealSubject();
}

this.realSubject.Request();
}

#endregion
}
}

// Output:
/*
Called RealSubject.Request()
*/

This
real-world code demonstrates the Proxy pattern for a Math object represented by a MathProxy object.

// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chimomo's Company" file="Program.cs">
// Respect the work.
// </copyright>
// <summary>
// Real-World Proxy Design Pattern.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace CSharpLearning
{
using System;

/// <summary>
/// Startup class for Real-World Proxy Design Pattern.
/// </summary>
internal static class Program
{
#region Methods

/// <summary>
/// Entry point into console application.
/// </summary>
private static void Main()
{
// Create math proxy
var 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));
}

#endregion
}

/// <summary>
/// The 'Subject interface
/// </summary>
public interface IMath
{
#region Public Methods and Operators

/// <summary>
/// The add.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
double Add(double x, double y);

/// <summary>
/// The div.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
double Div(double x, double y);

/// <summary>
/// The multiply.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
double Mul(double x, double y);

/// <summary>
/// The sub.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
double Sub(double x, double y);

#endregion
}

/// <summary>
/// The 'RealSubject' class
/// </summary>
internal class Math : IMath
{
#region Public Methods and Operators

/// <summary>
/// The add.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Add(double x, double y)
{
return x + y;
}

/// <summary>
/// The div.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Div(double x, double y)
{
return x / y;
}

/// <summary>
/// The multiply.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Mul(double x, double y)
{
return x * y;
}

/// <summary>
/// The sub.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Sub(double x, double y)
{
return x - y;
}

#endregion
}

/// <summary>
/// The 'Proxy Object' class
/// </summary>
internal class MathProxy : IMath
{
#region Fields

/// <summary>
/// The math.
/// </summary>
private Math math = new Math();

#endregion

#region Public Methods and Operators

/// <summary>
/// The add.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Add(double x, double y)
{
return this.math.Add(x, y);
}

/// <summary>
/// The div.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Div(double x, double y)
{
return this.math.Div(x, y);
}

/// <summary>
/// The multiply.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Mul(double x, double y)
{
return this.math.Mul(x, y);
}

/// <summary>
/// The sub.
/// </summary>
/// <param name="x">
/// The x.
/// </param>
/// <param name="y">
/// The y.
/// </param>
/// <returns>
/// The <see cref="double"/>.
/// </returns>
public double Sub(double x, double y)
{
return this.math.Sub(x, y);
}

#endregion
}
}

// Output:
/*
4 + 2 = 6
4 - 2 = 2
4 * 2 = 8
4 / 2 = 2
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息