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 ...
随机推荐
- Jupyter Notebook命令
Jupyter Notebook命令 1.启动 tensorboard --logdir=2017_05_03_00_12
- 用勤哲excel服务器开发设计燃烧器生产行业ERP
J公司是一家专业从事设计.制造.生产及销售各类燃油燃气燃烧设备和各类冶金燃烧装置的专业公司.2011年随着企业的发展,原来手工操作模式已经很难应付日益增长的工作量. J公司希望通过软件管理实现以下几个 ...
- Unity 移动 和 旋转 [小结]
[移动] Position: 说明: 直接修改位置数据 Translate: 说明: [匀速]朝着一个方向,一直移动. (dir * speed 可以控制速度)适合键盘控制物体上下左右运动 函数: ...
- BZOJ3620(kmp)
要点 本题使用\(O(n^2)\)的算法 外层枚举左端点,内层一直kmp到结尾,中间遇到合法的就ans++ 如果是acccca这种数据直接kmp过程顺手判断即可:但是aaa这种数据,j = 2,实际判 ...
- P3943 星空
传送门 观察题目数据,发现 k ≤ 8 ,可能可以从这里入手解决问题 考虑状态压缩 但是我们每次操作都会让一连串的序列改变,而序列的每个状态都是必须要知道的 很麻烦,所以考虑如何把一段区间表示地简单一 ...
- pandas关于其他merge用法(df1的的列索引和df2的行索引对应时候的)
- 通过id、classname定位元素,程序仍报找不到元素的原因
很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,用firebug也可以看到,就是定位不到,这种情况很有可能是frame在搞鬼.我们知道了原因,你现在就解决问题.sw ...
- VS 2017与 Docker
https://www.cnblogs.com/rufus-hua/p/6742836.html 参考 1 基于VS2017的Docker Support体检ASP.NET Core站点的Docker ...
- java动态数组笔记
动态数组: 在java.lang.reflect包下提供了Array类,包括一系列static方法,通过这些方法可动态的创建数组.给元素赋值.取出元素值等等 //理解数组引用——下面定义的objs数组 ...
- kubernetes命令补全
# yum install -y bash-completion # locate bash_completion /usr/share/bash-completion/bash_completion ...