zoukankan      html  css  js  c++  java
  • proxytheory.cs

      using System;

      // Proxy Pattern              Judith Bishop Dec 2006
      // Shows virtual and protection proxies
     
      class SubjectAccessor {  
        public interface ISubject {
        string Request ();
      }
     
      private class Subject {
        public string Request() {
          return "Subject Request " + "Choose left door\n";
        }
      }

      public class Proxy : ISubject {
        Subject subject;
     
        public string Request() {
          // A Virtual Proxy creates the object only on its first method call
          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";
            else
              subject = new Subject();
            return "Protection Proxy: Authenticated";
          }
          
          public string Request() {
             if (subject==null)
              return "Protection Proxy: Authenticate first";
            else
              return "Protection Proxy: Call to "+
            subject.Request();
          }  
        }
      }

      class Client  : SubjectAccessor {
        static void Main() {
          Console.WriteLine("Proxy Pattern\n");
        
          ISubject subject = new Proxy();
          Console.WriteLine(subject.Request());
          Console.WriteLine(subject.Request());
        
          subject = new ProtectionProxy();
          Console.WriteLine(subject.Request());
          Console.WriteLine((subject as ProtectionProxy).Authenticate("Secret"));
          Console.WriteLine((subject as ProtectionProxy).Authenticate("Abracadabra"));
          Console.WriteLine(subject.Request());
        }
      }

    /* Output:

    Proxy Pattern

    Subject inactive
    Subject active
    Proxy: Call to Subject Request Choose left door

    Subject active
    Proxy: Call to Subject Request Choose left door

    Protection Proxy: Authenticate first
    Protection Proxy: No access
    Protection Proxy: Authenticated
    Protection Proxy: Call to Subject Request Choose left door
    */
  • 相关阅读:
    jquery 的 outerWidth() 、width() 、innerWidth()
    图片自动切换 避免 鼠标快速滑过
    Ajax中日历控件的使用
    asp.net如何读取xml文件中的数据
    ASP.NET使用AspNetPager实现简单的分页功能
    XmlDataDocument与DataSet相互转化
    C#中如何过滤掉多余的html代码
    asp.net的几种经典的页面传值方法
    ASP.Net分页方法详解
    ASP.Net中省市级联有关
  • 原文地址:https://www.cnblogs.com/shihao/p/2496013.html
Copyright © 2011-2022 走看看