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 ...
随机推荐
- Oracle基本函数即字段拆分
--创建用户 CREATE USER jim IDENTIFIED BY changeit; --给用户赋登陆连接权限 GRANT CONNECT TO jim; --给用户赋资源权限 GRANT R ...
- python 字符串,bytes和hex字符串之间的相互转换
import binascii datastr='13'#string 类型转换为bytedataByte=str.encode(datastr)#byte串 转换为16进制 byte串 ,比如 b' ...
- luogu2948 滑雪课
题解里面全是dp的大神本蒟蒻瑟瑟发抖奉上一篇记忆化搜索... 其实嘛,记忆化搜索还是很安全透彻清真人品的,一般递推不好实现dp可以用记忆化搜索 然后本题先预处理一个mint[i]代表当前能力值为i,参 ...
- appium中driver.wait报IllegalMonitorStateException的解释
在写appium代码的时候,有的人想使用wait方法,写成:driver.wait(),结果抛出异常:IllegalMonitorStateException,看了appium client的api文 ...
- LUNA16数据集(一)简介
LUNA16,全称Lung Nodule Analysis 16,是16年推出的一个肺部结节检测数据集,旨在作为评估各种CAD(computer aid detection计算机辅助检测系统)的ban ...
- Java高级工程师应该掌握的东东
今天偶然看了膜拜单车官网对java程序员的招聘要求,如下,可以对照发现自己的不足 职责 负责APP SERVER中间层等模块开发 完成各类需求开发任务,同时保证服务稳定性.茁壮性 要求 精通Java语 ...
- SpringBoot---数据缓存(未完待续)
1.概述 1.1 在Spring中使用缓存的关键是配置CacheManager,而SpringBoot为我们自动配置了多个CacheManager的实现: 1.2 SpringBoot的CacheMa ...
- python绘制动态图
1.需要注意的问题 解决 MatplotlibDeprecationWarning: Using default event loop until function specific to this ...
- 统计学howto
统计之都 http://cos.name/ 让你拥有超能力:程序员应该掌握的统计学公式 Statistical Formulas For Programmers http://www.evanmill ...
- Abbott's Revenge UVA - 816 (输出bfs路径)
题目链接:https://vjudge.net/problem/UVA-816 题目大意: 有一个最多包含9*9 个交叉点的迷宫.输入起点,离开起点时的朝向和终点,求一条最短路(多解时任意输出 一个即 ...