zoukankan      html  css  js  c++  java
  • 软件工程 设计模式学习之代理模式Proxy

    今天说一下代理模式.
    从名字看大家都知道了代理模式,就是一个让一个类来帮你干某些事.是吧?嘿嘿.当然开始之前我们还是惯例来看看这个设计模式的UML:

    我看到了代理类请求其实调用的还是RealRequest类的请求,只是Proxy去调用


    --------------------------------------------------------------------------------
    我们现在来用代码说话了:
    第一个还是PHP

     1 abstract class CommRequest{   
     2     abstract public function Request();   
     3 }  
     4 
     5 abstract class CommRequest{
     6     abstract public function Request();
     7 }
     8 class ActualRequest extends CommRequest{   
     9     public function Request() {   
    10     echo("Actual Request !\n");   
    11     }  
    12 
    13 class ActualRequest extends CommRequest{
    14     public function Request() {
    15  echo("Actual Request !\n");
    16     }
    17 class PHPProxy extends CommRequest {   
    18     private $realRequest;   
    19     public function Request() {   
    20         if(isset($this->realRequest)){   
    21             echo("Proxy Here!\n");   
    22             $this->realRequest->Request();   
    23         }   
    24     }   
    25 }  
    26 
    27 class PHPProxy extends CommRequest {
    28  private $realRequest;
    29  public function Request() {
    30   if(isset($this->realRequest)){
    31    echo("Proxy Here!\n");
    32    $this->realRequest->Request();
    33   }
    34  }
    35 
    36 
    37 $p = new PHPProxy();
    38 $p->Request();
    39 

    --------------------------------------------------------------------------------
    接下来来看看C#的代码.

      1 namespace Proxy_DesignPattern   
      2 {   
      3     using System;   
      4     using System.Threading;   
      5   
      6     /// <SUMMARY></SUMMARY>   
      7     ///    Summary description for Client.   
      8     ///    
      9     abstract class CommonSubject   
     10     {   
     11         abstract public void Request();   
     12     }   
     13   
     14     class ActualSubject : CommonSubject   
     15     {   
     16         public ActualSubject()   
     17         {   
     18             // Assume constructor here does some operation that takes quite a   
     19             // while - hence the need for a proxy - to delay incurring this    
     20             // delay until (and if) the actual subject is needed   
     21             Console.WriteLine("Starting to construct ActualSubject");   
     22             Thread.Sleep(1000); // represents lots of processing!    
     23             Console.WriteLine("Finished constructing ActualSubject");   
     24         }   
     25   
     26         override public void Request()   
     27         {   
     28             Console.WriteLine("Executing request in ActualSubject");   
     29         }   
     30     }   
     31   
     32     class Proxy : CommonSubject   
     33     {   
     34         ActualSubject actualSubject;   
     35   
     36         override public void Request()   
     37         {   
     38             if (actualSubject == null)   
     39                 actualSubject = new ActualSubject();   
     40             actualSubject.Request();   
     41         }   
     42   
     43     }   
     44   
     45     public class Client   
     46     {   
     47         public static int Main(string[] args)   
     48         {   
     49             Proxy p = new Proxy();   
     50   
     51             // Perform actions here   
     52             // . . .    
     53   
     54             if (1 == 1)     // at some later point, based on a condition,    
     55                 p.Request();// we determine if we need to use subject   
     56   
     57             return 0;   
     58         }   
     59     }   
     60 }  
     61 
     62 namespace Proxy_DesignPattern
     63 {
     64     using System;
     65     using System.Threading;
     66 
     67     /// 
     68     ///    Summary description for Client.
     69     /// 
     70     abstract class CommonSubject
     71     {
     72         abstract public void Request();
     73     }
     74 
     75     class ActualSubject : CommonSubject
     76     {
     77         public ActualSubject()
     78         {
     79             // Assume constructor here does some operation that takes quite a
     80             // while - hence the need for a proxy - to delay incurring this 
     81             // delay until (and if) the actual subject is needed
     82             Console.WriteLine("Starting to construct ActualSubject");
     83             Thread.Sleep(1000); // represents lots of processing! 
     84             Console.WriteLine("Finished constructing ActualSubject");
     85         }
     86 
     87         override public void Request()
     88         {
     89             Console.WriteLine("Executing request in ActualSubject");
     90         }
     91     }
     92 
     93     class Proxy : CommonSubject
     94     {
     95         ActualSubject actualSubject;
     96 
     97         override public void Request()
     98         {
     99             if (actualSubject == null)
    100                 actualSubject = new ActualSubject();
    101             actualSubject.Request();
    102         }
    103 
    104     }
    105 
    106     public class Client
    107     {
    108         public static int Main(string[] args)
    109         {
    110             Proxy p = new Proxy();
    111 
    112             // Perform actions here
    113             // . . . 
    114 
    115             if (1 == 1)  // at some later point, based on a condition, 
    116                 p.Request();// we determine if we need to use subject
    117 
    118             return 0;
    119         }
    120     }
    121 }
    122 
    123 

    --------------------------------------------------------------------------------
    最后的舞台还是留给Delphi了.

     1 type 
     2   ICommRequest = interface 
     3     procedure Request; 
     4   end
     5 
     6   TActualRequest = class(TInterfacedObject,ICommRequest) 
     7   public 
     8     procedure Request; 
     9   end
    10 
    11   TProxy = class(TInterfacedObject,ICommRequest) 
    12   private 
    13     FRealRequest:TActualRequest; 
    14   public 
    15     procedure Request; 
    16     property RealRequest:TActualRequest  read FRealRequest write FRealRequest; 
    17   end
    18 
    19 { TProxy } 
    20 
    21 procedure TProxy.Request; 
    22 begin 
    23   if Assigned(RealRequest) then 
    24   begin 
    25     Writeln('Proxy!'); 
    26     RealRequest.Request; 
    27   end
    28 end
    29 
    30 { TActualRequest } 
    31 
    32 procedure TActualRequest.Request; 
    33 begin 
    34   Writeln('Actual Request'); 
    35 end
    36 
    37 var 
    38   tmp:string
    39   P:TProxy; 
    40   A:TActualRequest; 
    41 begin 
    42   P:=TProxy.Create; 
    43   A:=TActualRequest.Create; 
    44   try 
    45     P.RealRequest:=A; 
    46     P.Request; 
    47     Readln(tmp); 
    48   finally 
    49     A.Free; 
    50     P.Free; 
    51   end
    52 end
    53 

    好的.其实这个模式很简单,希望对大家有帮助.

  • 相关阅读:
    Java中的File类
    scala语法
    Spark核心原理
    资源调度器
    YARN工作机制
    MapReduce原理和工作过程
    序列化
    Hbase(2)表的设计和Rowkey等的设计
    Hbase(1)架构和工作原理
    Exception in thread "main" java.lang.NoSuchMethodError: io.netty.buffer.PooledByteBufAllocator.metric()Lio/netty/buffer/PooledByteBufAllocatorMetric;
  • 原文地址:https://www.cnblogs.com/huangjacky/p/1618922.html
Copyright © 2011-2022 走看看