zoukankan      html  css  js  c++  java
  • 设计模式——代理模式

    名称 Proxy
    结构  
    意图 为其他对象提供一种代理以控制对这个对象的访问。
    适用性
    • 在需要用比较通用和复杂的对象指针代替简单的指针的时候,使用P r o x y 模式。下面是一 些可以使用P r o x y 模式常见情况:
      1) 远程代理(Remote Proxy )为一个对象在不同的地址空间提供局部代表。 NEXTSTEP[Add94] 使用N X P r o x y 类实现了这一目的。Coplien[Cop92] 称这种代理为“大使” (A m b a s s a d o r )。
      2 )虚代理(Virtual Proxy )根据需要创建开销很大的对象。在动机一节描述的I m a g e P r o x y 就是这样一种代理的例子。
      3) 保护代理(Protection Proxy )控制对原始对象的访问。保护代理用于对象应该有不同 的访问权限的时候。例如,在C h o i c e s 操作系统[ C I R M 9 3 ]中K e m e l P r o x i e s 为操作系统对象提供 了访问保护。
      4 )智能指引(Smart Reference )取代了简单的指针,它在访问对象时执行一些附加操作。 它的典型用途包括:
    • 对指向实际对象的引用计数,这样当该对象没有引用时,可以自动释放它(也称为S m a r tP o i n t e r s[ E d e 9 2 ] )。
    • 当第一次引用一个持久对象时,将它装入内存。
    • 在访问一个实际对象前,检查是否已经锁定了它,以确保其他对象不能改变它。
    Code Example
    // Factory Method

    // Intent: "Provide a surrogate or placeholder for another object to 
    // control access to it". 

    // For further information, read "Design Patterns", p207, Gamma et al.,
    // Addison-Wesley, ISBN:0-201-63361-2

    /* Notes:
     * When there is a large CPU/memory expense attached to handling an object
     * directly, it can be useful to use a lightweight proxy in front of it, 
     * which can take its place until the real object is needed. 
     
    */

     
    namespace Proxy_DesignPattern
    {
        
    using System;
        
    using System.Threading;

        
    /// <summary>
        
    ///    Summary description for Client.
        
    /// </summary>

        abstract class CommonSubject 
        
    {
            
    abstract public void Request();        
        }


        
    class ActualSubject : CommonSubject
        
    {
            
    public ActualSubject()
            
    {
                
    // Assume constructor here does some operation that takes quite a
                
    // while - hence the need for a proxy - to delay incurring this 
                
    // delay until (and if) the actual subject is needed
                Console.WriteLine("Starting to construct ActualSubject");        
                Thread.Sleep(
    1000); // represents lots of processing! 
                Console.WriteLine("Finished constructing ActualSubject");
            }

                
                
    override public void Request()
            
    {
                Console.WriteLine(
    "Executing request in ActualSubject");
            }

        }


        
    class Proxy : CommonSubject
        
    {
            ActualSubject actualSubject;

            
    override public void Request()
            
    {
                
    if (actualSubject == null)
                    actualSubject 
    = new ActualSubject();
                actualSubject.Request();
            }
        
            
        }

        
        
    public class Client
        
    {
            
    public static int Main(string[] args)
            
    {
                Proxy p 
    = new Proxy();

                
    // Perform actions here
                
    // . . . 

                
    if (1==1)        // at some later point, based on a condition, 
                    p.Request();// we determine if we need to use subject
                                    
                
    return 0;
            }

        }

    }



  • 相关阅读:
    [转发]深入理解git,从研究git目录开始
    iOS系统网络抓包方法
    charles抓包工具
    iOS多线程中performSelector: 和dispatch_time的不同
    IOS Core Animation Advanced Techniques的学习笔记(五)
    IOS Core Animation Advanced Techniques的学习笔记(四)
    IOS Core Animation Advanced Techniques的学习笔记(三)
    IOS Core Animation Advanced Techniques的学习笔记(二)
    IOS Core Animation Advanced Techniques的学习笔记(一)
    VirtualBox复制CentOS后提示Device eth0 does not seem to be present的解决方法
  • 原文地址:https://www.cnblogs.com/DarkAngel/p/204373.html
Copyright © 2011-2022 走看看