zoukankan      html  css  js  c++  java
  • Design Pattern ->Bridge

    Layering & Contract Philosophy With additional indirection.


     1 class CWindowImp
     2 {  
     3     public: virtual void DrawLine(){};
     4         public: virtual void DrawText(){};
     5 }
     6 class CWindow
     7 {
     8       public: virtual void DrawLine(){};
     9                  virtual void DrawText(){};
    10       protect: WindowImp* GetWindowImp()
    11       { 
    12             return WindowSystemFactory::Instance()->MakeWindwImp();
    13        } ;
    14        private: WindowImp *pWindowImp;
    15 }
    16 class CXPWindow: public CWindow
    17 {
    18          public: void DrawLine() 
    19          { 
    20                GetWindowImp()->DrawLine();
    21          }
    22 }
    23 class CMacWindow: public CWindow
    24 {
    25           public: void DrawLine() 
    26          { 
    27              do_something_special; GetWindowImp()->DrawLine();
    28           }
    29 class CXPWindowImp: public CWindowImp
    30 class CMacWindowImp: public CWindowImp

    Applicability

    Use the Bridge pattern when

    • you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time.
    • Layering or interface-Based/Oriented programming. Separate the layer.
    • Both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently.
    • Changes in the implementation of an abstraction should have no impact/effect/influence on clients; that is, their code should not have to be recompiled.
    • (C++) you want to hide the implementation of an abstraction completely from clients. In C++ the representation of a class is visible in the class interface.
    • You have a proliferation of classes as shown earlier in the first Motivation diagram. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" [RBP+91] to refer to such class hierarchies.
    • You want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class [Cop92], in which multiple objects can share the same string representation (StringRep).

    Participants

    • Abstraction (Window)Defines the abstraction's interface.Maintains a reference to an object of type Implementor.
    • RefinedAbstraction (IconWindow)Extends the interface defined by Abstraction.
    • Implementor (WindowImp)  Defines the interface for implementation classes. This interface doesn't have to correspond exactly to Abstraction's interface; in fact the two interfaces can be quite different. Typically the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives.
    • ConcreteImplementor (XWindowImp, PMWindowImp)Implements the Implementor interface and defines its concrete implementation.

    Collaborations

    • Abstraction forwards client requests to its Implementor object.

    Consequences

    The Bridge pattern has the following consequences:

    • Decoupling interface and implementation. An implementation is not bound permanently to an interface. The implementation of an abstraction can be configured at run-time. It's even possible for an object to change its implementation at run-time. Decoupling Abstraction and Implementor also eliminates compile-time dependencies on the implementation. Changing an implementation class doesn't require recompiling the Abstraction class and its clients. This property is essential when you must ensure binary compatibility between different versions of a class library. Furthermore, this decoupling encourages layering that can lead to a better-structured system. The high-level part of a system only has to know about Abstraction and Implementor.
    • Improved extensibility. You can extend the Abstraction and Implementor hierarchies independently.
    • Hiding implementation details from clients. You can shield clients from implementation details, like the sharing of implementor objects and the accompanying reference count mechanism (if any).
  • 相关阅读:
    【原】费马小定理(Fermat little theorem)详解
    【原】水库抽样详解
    【原】模幂运算(Modular Exponentiation)算法
    【原】 POJ 3630 Phone List Trie树 解题报告
    【Joke】你可以去当程序员了
    【原】 POJ 3750 小孩报数问题 Joseph相关问题详解 解题报告
    【原】 POJ 3748 位操作 解题报告
    react 性能优化
    修改jsp文件,访问时没有变化。可能是修改了系统的时间,,,郁闷呢
    在Windows 7 下使用Visual Studio 2010 编写自动申请管理员权限运行的程序
  • 原文地址:https://www.cnblogs.com/iiiDragon/p/3268451.html
Copyright © 2011-2022 走看看