Proxy Pattern - C# 3.0 Design Patterns

Proxy Pattern属于Structural Patterns

介绍:

Proxy Pattern的作用是为其他对象提供一个代理以控制对这个对象的访问,如何你的对象创建非常昂贵;需要访问控制;是一个远程对象或者调用之前需要采取一些其他操作,那么你可以考虑为该对象创建一个代理。

设计于实现:

QQ截图20120316154658

 

ISubject:一个公共的接口供代理和被代理对象继承

Subject:需要被代理的对象

Proxy:代理对象 A class that creates, controls, enhances, and authenticates access to a Subject

 

    public interface ISubject
    {
        string Request();
    }

    public class Subject : ISubject
    {
        #region ISubject Members
        public string Request()
        {
            return "Subject Request ";
        }
        #endregion
    }

    public class Proxy : ISubject
    {
        Subject subject;
        public string Request()
        {
            if (subject == null)
            {
                Console.WriteLine("Subject inactive");
                subject = new Subject();
            }
            Console.WriteLine("Subject active");
            return "Proxy: Call to " + subject.Request();
        }
    }
    public class ProtectionProxy : ISubject
    {
        // An authentication proxy first asks for a password
        Subject subject;
        string password = "Abracadabra";

        public string Authenticate(string supplied)
        {
            if (supplied != password)
            {
                return "Protection Proxy: No access";
            }
            subject = new Subject();
            return "Protection Proxy: Authenticated";
        }

        public string Request()
        {
            if (subject == null)
            {
                return "Protection Proxy: Authenticate first";
            }
            return "Protection Proxy: Call to " + subject.Request();
        }
    }

    [TestFixture]
    public class ProxyPatternTest
    {
        [Test]
        public void T1()
        {
            ISubject subject = new Proxy();
            Console.WriteLine(subject.Request());

            ISubject subject2 = new ProtectionProxy();

            Console.WriteLine(subject2.Request());

            Console.WriteLine((subject2 as ProtectionProxy).Authenticate("Abracadabra"));
            Console.WriteLine(subject2.Request());

        }
    }

Decorator Pattern 和 Proxy Pattern 的区别:

Decorator Pattern 有"Smart Proxy" 可见两者有相同之处,在Decorator Pattern中装饰者和被装饰者都实现了相同的接口,这个Proxy Pattern一样代理和被代理的对象也同样实现了相同的接口。同时装饰者和代理都可以对实际的对象进行控制。

但是两者最显著的区别是Proxy Pattern是在代理类内部创建对象的实例,而Decorator Pattern是通过参数传递对象的实例。

 

var component = new Component();
//Component是通过构造函数的方式传递给DecoratorB的
var decoratorB = new DecoratorB(component);

 

public class Proxy : ISubject
{
       Subject subject;
       public string Request()
       {
           if (subject == null)
           {
               Console.WriteLine("Subject inactive");
              //在代理的内部创建类(Subject)的实例
               subject = new Subject();
           }

           …
       }
   }

参考文章:

1、The differences between Decorator Pattern and Proxy Pattern

2、How do the Proxy, Decorator, Adaptor, and Bridge Patterns differ?

posted @ 2012-03-16 16:10 MicroCoder 阅读(...) 评论(...) 编辑 收藏