Design Pattern ->Bridge
Layering & Contract Philosophy With additional indirection.

class CWindowImp
{
public: virtual void DrawLine(){};
public: virtual void DrawText(){};
}
class CWindow
{
public: virtual void DrawLine(){};
virtual void DrawText(){};
protect: WindowImp* GetWindowImp()
{
return WindowSystemFactory::Instance()->MakeWindwImp();
} ;
private: WindowImp *pWindowImp;
}
class CXPWindow: public CWindow
{
public: void DrawLine()
{
GetWindowImp()->DrawLine();
}
}
class CMacWindow: public CWindow
{
public: void DrawLine()
{
do_something_special; GetWindowImp()->DrawLine();
}
class CXPWindowImp: public CWindowImp
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).
Design Pattern ->Bridge的更多相关文章
- Design Pattern Bridge 桥设计模式
桥设计模式事实上就是一个简单的has a relationship.就是一个类拥有还有一个类,并使用还有一个类实现须要的功能. 比方遥控器和电视之间能够使用桥设计模式达到能够使用同一个遥控器控制多台电 ...
- 说说设计模式~大话目录(Design Pattern)
回到占占推荐博客索引 设计模式(Design pattern)与其它知识不同,它没有华丽的外表,没有吸引人的工具去实现,它是一种心法,一种内功,如果你希望在软件开发领域有一种新的突破,一个质的飞越,那 ...
- 设计模式(Design Pattern)系列之.NET专题
最近,不是特别忙,重新翻了下设计模式,特地在此记录一下.会不定期更新本系列专题文章. 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用 ...
- [转]Design Pattern Interview Questions - Part 4
Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, COR Pattern, Proxy Pattern, te ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- C++ Design Pattern: What is a Design Pattern?
Q: What is a Design Pattern? A: Design Patterns represent solutions to problems what arise when deve ...
- Design Pattern in Simple Examples
Instead of defining what is design pattern lets define what we mean by design and what we mean by pa ...
- java设计模式大全 Design pattern samples in Java(最经典最全的资料)
java设计模式大全 Design pattern samples in Java(最经典最全的资料) 2015年06月19日 13:10:58 阅读数:11100 Design pattern sa ...
- [转]Design Pattern Interview Questions - Part 2
Interpeter , Iterator , Mediator , Memento and Observer design patterns. (I) what is Interpreter pat ...
随机推荐
- 10.20 olinr
感谢olinr提供md文件 免得我整理格式了 1.求助 (help.cpp/c/pas) [问题背景] 马上就要noip了,lrt同志\(\displaystyle\begin{vmatrix}\te ...
- 关于IBOutlet的生命周期
在调试程序的时候,发现 IBOutlet的对象在执行 getter时,开始一直想找IBOutlet对象getter方法前被执行的代码,但是一直找不到,于是我就想是不是系统自动给IBOutlet 自动初 ...
- (转)TestNG框架提供两种传入参数的方法:
1.从testng.xml传入参数. 如果参数是一些简单的值,可以直接在testng.xml中定义.这也是最常用的一种. 可以在测试用例中给参数一个默认值.这样,即使在xml文件中没有这个变量,你的测 ...
- react PropTypes 与 DefaultProps
PropTypes 与 DefaultProps import React ,{ Component } from 'react'; import PropTypes from 'prop-types ...
- maven 子父工程。。。
子工程module 父工程 主要是注意路径问题..否则会报错---
- C#空接合操作符——??
操作符: ?? 用法:C = A ?? B; 解释:if(A != null){ C=A;} else{C=B} 类似三元运算符 :? 例子: Int32? num1=null; Int32? ...
- C#工具类之字符串扩展类
/// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...
- python 面向对象及封装继承和多态
######装饰器######装饰器的概念 - 装饰器的实现是函数里面嵌套函数;- 装饰器的本质是一个函数, 它可以让其他函数在不需要做任何代码改动的前提下增加额外的功能;- 装饰器需要传递一个函数, ...
- SQL注入工具sqlmap的注入过程记录
1.sqlmap的get注入 假设目标是 https://www.baidu.com/news.php?id=1&data=2 sqlmap语句 列库 sqlmap.py -u "h ...
- spring boot——关于一个Mysql主键的问题
问题是这样的: 我现在有一个被@Entity标记的类TimeLine,其中id为主键. TimeLineController中有一个接收post请求的add()方法,这个方法会接受客户端传来的一个表单 ...