150 likes | 266 Views
The Bridge Pattern. Guang Hu February 2002. Overview. Motivation Participants Structure Applicability Benefits Drawbacks Related Pattern. Entry. oracleDBEntry. FilesysEntry. Motivation. Entry. Appointment. Task. OracleDBApp. FilesysApp. OracleDBTask. FilesysTask. Motivation.
E N D
The Bridge Pattern Guang Hu February 2002
Overview • Motivation • Participants • Structure • Applicability • Benefits • Drawbacks • Related Pattern
Entry oracleDBEntry FilesysEntry Motivation
Entry Appointment Task OracleDBApp. FilesysApp. OracleDBTask FilesysTask Motivation
Task getPriority() setPriority() getText() setText() Destroy() Appointment getAlarm() setAlarm() getText() setText() Destroy() PersistentImp. initialize() store() load() Destroy() Entry getText() setText() Destroy() impl Bridge OraclePImp initialize() store() load() destroy() AccessPImp initialize() store() load() destroy() Motivation
Participants • Abstraction (Entry): - define the abstraction’s interface - maintains a reference to an object of type Implementor • Refined Abstraction (Task): - extends the interface defined by Abstraction
Participants (continue) • Implementor (persistentImp): - defines an interface for implementation classes. Typically, this Implementor’s interface provides only primitive menthod • ConcreteImplementor (oraclePImp, AccessPImp): - implements the Implementor’s interface
Client Implementer Abstraction impl OperationImp() ConcreteImplementerB RefinedAbstraction ConcreteImplementerA Structure
Applicability • Want toavoid a permanent binding between an abstraction and implementation. • When abstractions and implementations should be extensible through subclassing. • When implementation changes should not impact clients.
Applicability (continue) • When the implementation should be completely hidden from the client. (C++) • When you have a proliferation of classes. • When, unknown to the client, implementations are shared among objects.
Benefits • Avoid permanent binding between an abstraction and its implementation • Avoid nested generalizations • Ease adding new implementations • Reduce code repetition • Allow runtime switching of behaviour
Drawbacks • Double indirection - “entry”operation are implemented by subclasses ofPersistentImp class. Entryclass must delegate the message to a PersistentImp subclass which implements the appropriate method. This will have a slight impact on performance.
Consequences • decoupling interface & implementation - implementation of abstraction - can be configured at run-time - eliminate compile time dependencies on implementation - encourages layering • improved extensibility - Abstraction & Implementer - can be extended independently • hiding implementation details from clients
Related Pattern • Abstract Factory- The Abstract Factory pattern can be used by the Bridge pattern to decide which implementation class to instantiate for an abstraction object. • Adapter