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

这里写图片描述

猜你喜欢

转载自blog.csdn.net/yffhhffv/article/details/83580836
今日推荐