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
    */
  • 相关阅读:
    范仁义html+css课程---7、表单
    范仁义html+css课程---6、表格
    范仁义html+css课程---5、列表
    范仁义html+css课程---4、文本标签
    范仁义html+css课程---3、图片和超链接
    react项目如何运行
    maven search
    PowerDesigner中Name与Code同步的问题
    PowerDesigner跟表的字段加注释
    MobilePhone正则表达式
  • 原文地址:https://www.cnblogs.com/shihao/p/2496013.html
Copyright © 2011-2022 走看看