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).
  • 相关阅读:
    layui学习
    网络安装Centos x64 6.10
    给没有连接因特网的centos使用yum安装其他软件。
    Java使用线程池
    记录一次查看后台是否在运行资源备份上报到华为云存储的过程
    潭州课堂python
    什么是动态规划?动态规划的意义是什么?
    南明区教师信息管理系统之审批流程设计思路
    连接慢的主要原因是DNS解析导致
    jfinal中,render的时候如何取到view根目录
  • 原文地址:https://www.cnblogs.com/iiiDragon/p/3268451.html
Copyright © 2011-2022 走看看