Adapter,Bridge,Facade,Proxy。它们都是Structural Patterns,它们的关系如下图所示:  
             
              
             
            ----------------------------------Adapter---------------------------- 
             
            ●Tip 1:关键字。existing,reuse。  
             
            ●Tip 2:图。  
             
              
             
            可以看到,Adapter是在Target和Adaptee已经existing的情况下,临危受命的,是“事后工程”。 
             
            ●Tip 3:实现和使用。到底使用Class Adapter还是Object  
            Adapter,要视不同情况而定。 
             
            Class Adapter优点:Lets Adapter override some of Adaptee's  
            behavior, since Adapter is a subclass of Adaptee。另外,不必引入额外的实例化问题,Introduces  
            only one object, and no additional pointer indirection is needed to  
            get to the adaptee。  
             
            Class Adapter缺点:Adapts Adaptee to Target by committing to a  
            concrete Adapter class. As a consequence, a class adapter won't work  
            when we want to adapt a class and all its subclasses.  
             
            Object Adapter优点:Lets a single Adapter work with many Adaptees─that  
            is, the Adaptee itself and all of its subclasses (if any). The  
            Adapter can also add functionality to all Adaptees at once.  
             
            Object Adapter缺点:Makes it harder to override Adaptee behavior.  
             
            MFC本身就是Adapter的例子,Win32 API是基于func的,MFC是基于class的。  
             
            ●Tip 4:优点。可用于整合遗留系统。  
             
            ●Tip 5:局限性。如果遗留系统还在发展、变化和增长之中,整合的开销会很大。  
             
            ----------------------------------Bridge---------------------------- 
             
            ●Tip 1:关键字。Abstraction and its Implementation。  
             
            ●Tip 2:图。  
             
              
             
            可以看到,我把图分成了4个Layer:Application,Abstraction,Logic,Implementor。 
             
            Application层的Client使用Abstraction层的抽象对象,这些抽象对象是和具体平台无关的;Abstraction层的抽象对象又是由Implementor层的对象实现的,和具体平台有关的对象在Implementor层;为了更加清晰地说明问题,我在Abstraction层和Implementor层之间增加了Logic层,该层是Implementor如何实现Abstraction的程序逻辑。 
             
            其实,Bridge模式是非常典型的Layer-like模式。为了对比方便,我放一张ET++的Layer图在下边。 
             
              
             
            ●Tip 3:实现和使用。  
             
            在此集中讨论一个问题:RefinedImplementor的实例化问题。因为RefinedAbstraction肯定是由Client实例化的,但RefinedImplementor的实例化却可以分为2种情况: 
             
            第1种情况,由Abstraction或RefinedAbstraction实例化。这要求Abstraction或RefinedAbstraction知道所有的RefinedImplementor,具体实例化哪一个,可以通过Abstraction::Abstraction(para)的参数来确定。 
             
            第2种情况,委托给别的对象来实例化。典型的,可以委托给一个Abstract  
            Factory来实例化。这样,Abstraction只需要知道Implementor这个Interface,这是一个良性依赖,在图中被我画成了绿色。 
             
            ET++中,和平台无关的Window是用和平台相关的WindowPort实现的,但后者的实例化是委托(delegate)WindowSystem这个Abstract  
            Factory来完成的: 
             
            
            class Window {
...
protected:
  WindowImp* GetWindowImp(); ///////////////call Abstract Factory and return Implementor
private:
  WindowImp* _imp;  ////////////save GetWindowImp() 's return value
...
};
WindowImp* Window::GetWindowImp () {
  if (_imp == 0) {
    _imp = WindowSystemFactory::Instance()->MakeWindowImp();////////////////////WindowSystemFactory is a Singleton
  }
  return _imp;
}
              
             
            ●Tip 4:支持变化。Putting the  
            Window abstraction and its implementation in separate class  
            hierarchies。You can extend the Abstraction and Implementor  
            hierarchies independently。图中的黄色Class就是假想后来扩充的。  
             
            ----------------------------------Proxy---------------------------- 
             
            ●Tip 1:关键字。Placeholder,Control。  
             
            ●Tip 2:图。  
             
              
             
            可以看到,Proxy和Realthing的对外接口是相同的。 
             
            ●Tip 3:实现和使用。讨论两个关键字:  
             
            Placeholder。可以是a direct reference to its real subject,比如在同一台PC上(且在同一个Application内),DrawProxy之间调用Draw;也可以是only  
            an indirect reference,比如跨网络的应用,可能只知道“host  
            ID and local address on host”,哈哈,就是“IP地址+端口号”。  
             
            Control。之所以Proxy,就是为了能Control,或者说为了智能:智能保护,智能拒绝,智能回收,智能降低开销。 
             
            在COM中,有智能指针SmartPointer。.Net中的SmartClient说不定也是。。。 
             
            ----------------------------------Facade---------------------------- 
             
            ●Tip 1:关键字。Subsystem,Higher-level Interface。  
             
            ●Tip 2:图。  
             
              
             
            可以看到,Facade封装了多个Class。 
             
            ●Tip 3:实现和使用。  
             
            在Facade Class上还可以做些文章,以进一步降低耦合度,比如Facade本身可以派生Subclass,或者用委托(delegate)来配置Facade。The  
            coupling between clients and the subsystem can be reduced even  
            further by making Facade an abstract class with concrete subclasses  
            for different implementations of a subsystem. Then clients can  
            communicate with the subsystem through the interface of the abstract  
            Facade class. This abstract coupling keeps clients from knowing  
            which implementation of a subsystem is used. An alternative to  
            subclassing is to configure a Facade object with different subsystem  
            objects. To customize the facade, simply replace one or more of its  
            subsystem objects.  
             
            使用名字空间。A class encapsulates state and operations,  
            while a subsystem encapsulates classes. The C++ standardization  
            committee added name spaces to the language [], which will let you  
            expose just the public subsystem classes.  
             
            在ET++中,有个称为browsing tools的Subsystem,其中的ProgrammingEnvironment就是Facade。ET++中的相关研究,请参考本站(lcspace.nease.net)的Framework栏目。  
             
            ●Tip 4:支持变化。Lets you vary the components of the  
            subsystem without affecting its clients。  
             
            ----------------------------------Proxy and  
            Decorator---------------------------- 
             
            ●Tip 1:Proxy是Decorator特例。  
             
            当一个Decorator模式,Decorator不能递归修饰Decorator,而且也不要before  
            forwarding和after forwarding的操作了,仅仅就是forward,Decorator模式也就退化成Proxy模式了。  
             
            ----------------------------------Adapter,Bridge,Facade and  
            Proxy---------------------------- 
             
            Adapter,Bridge,Facade and Proxy这4种模式,其实都可以归为“Layer-like模式”。想想看,它们确实都是“Layer间单向调用服务”的。  
             
            当然,从逻辑上来讲,这4个模式又分为2组: 
             
            Bridge and Facade──涉及逻辑层次“不同”的2个Layer。  
             
            Adapter and Proxy──涉及逻辑层次“相同”的2个Layer。  
             
            下面是典型的Layer-like模式的示意图: 
             
              
            
              
                                                            
              上一页      下一页 
           |