Design Pattern - Proxy(C#)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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 = 64 - 2 = 24 * 2 = 84 / 2 = 2*/
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值