0% found this document useful (0 votes)
68 views18 pages

Design Patterns: Structural Pattern - Bridge

This document discusses the Bridge design pattern. The Bridge pattern decouples an abstraction from its implementation, allowing them to vary independently. It is applicable when abstraction and implementation need to be extended independently. The pattern defines an interface for the abstraction and concrete classes for various implementations. An abstraction delegates method calls to its implementation object through the interface. This decoupling allows the same abstraction to work with multiple implementations. The document provides an example of applying Bridge to decouple window abstractions from platform-specific implementations.

Uploaded by

ST'z Jia Wei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views18 pages

Design Patterns: Structural Pattern - Bridge

This document discusses the Bridge design pattern. The Bridge pattern decouples an abstraction from its implementation, allowing them to vary independently. It is applicable when abstraction and implementation need to be extended independently. The pattern defines an interface for the abstraction and concrete classes for various implementations. An abstraction delegates method calls to its implementation object through the interface. This decoupling allows the same abstraction to work with multiple implementations. The document provides an example of applying Bridge to decouple window abstractions from platform-specific implementations.

Uploaded by

ST'z Jia Wei
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Design Patterns

CT070-3-3 Version 0418

Structural Pattern – Bridge


Topic & Structure of The
Lesson

• Bridge Design Pattern


– Intent
– Motivation
– Applicability
– Definition

CT070-3-3 - Design Patterns Bridge


Learning Outcomes

At the end of this session, you should be


able to;

– Describe benefits of Bridge pattern


– Recognise and apply Bridge pattern given
a scenario

CT070-3-3 - Design Patterns Bridge Slide 2 (of 17)


Key Terms You Must Be
Able To Use
• If you have mastered this topic, you should
be able to use the following terms
correctly in your assignments and exams:
– Decouple
– Abstraction
– Implementation

CT070-3-3 - Design Patterns Bridge


Description - Bridge

Intent:
Decouple an abstraction from its implementation so
that the two can vary independently
Also Known As:
Handle/Body
Motivation:
When an abstraction can have one of several
implementations, the usual way to accommodate them
is to use inheritance. An abstract class defines the
interface to the abstraction, and concrete subclasses
implement it in different ways.

CT070-3-3 - Design Patterns Bridge Slide 4 (of 17)


Description - Bridge

Applicability:
Use 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.
- Both the abstractions and their implementations should be
extensible by sub-classing. 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 on clients; that is, their code should not have to be
compiled.

CT070-3-3 - Design Patterns Bridge Slide 5 (of 17)


Definition - Bridge

The BRIDGE pattern decouples an


abstraction from its implementation so
that the two can vary independently

CT070-3-3 - Design Patterns Bridge Slide 6 (of 17)


Bridge

• An abstraction with concrete methods that


delegate to an implementation object

CT070-3-3 - Design Patterns Bridge Slide 7 (of 17)


Refactoring to a Bridge

1) Move abstract operations in the


superclass into an interface.
2) Define classes that implement the
interface.
3) Remaining methods in the abstract class
operate on an instance of the new
interface.

CT070-3-3 - Design Patterns Bridge Slide 8 (of 17)


Case Study

Windows abstraction covers different kinds


of windows or new platforms. For every
new window created – example
IconWindow, TansientWindow, there has to
be two platforms created – XIconWindow
and PMIconWindow. This is extremely
cumbesome for imageing we’ll have to
define two classes for very kind of window!

CT070-3-3 - Design Patterns Bridge Slide 10 (of 17)


Case Study (cont …)

Window

drawText()
drawRect()
devDrawText()
devDrawLine()

XWindow PMWindow IconWindow

XIconWindow PMIconWindow

CT070-3-3 - Design Patterns Bridge Slide 11 (of 17)


Case Study (cont…)

How to we apply a Bridge pattern in


this instance?

CT070-3-3 - Design Patterns Bridge Slide 12 (of 17)


Case Study (cont ..)

Window <<Interface>>
WindowImp
drawText()
drawRect() devDrawText()
devDrawText() devDrawLine()
devDrawLine()

XWindowImp PMWindowIMP
IconWindow TransientWindow
devDrawText() devDrawLine()
drawBorder() drawCloseBox()
devDrawLine() devDrawText()

CT070-3-3 - Design Patterns Bridge Slide 13 (of 17)


Case Study (cont ..)

• The diagram now decouples the window


abstractions from the various platform-specific
implementation.

• Therefore, both Window and WindowImp


behaves as a bridge, because it bridges the
abstraction and its implementation, letting them
vary independently.

CT070-3-3 - Design Patterns Bridge Slide 14 (of 17)


Summary of Main Teaching
Points

• Bridge decouples an abstraction from its


implementation so that the two can vary
independently.

CT070-3-3 - Design Patterns Bridge Slide 15 (of 17)


Question and Answer Session

Q&A

CT070-3-3 - Design Patterns Bridge Slide 16 (of 17)


What we will cover next

• Structural Pattern - Decorator

CT070-3-3 - Design Patterns Bridge


References

Steven John Metsker, Design Patterns Java


Workbook, Addison Wesley

Erich Gamma et. al., Design Patterns –


Elements of Reusable Object-Oriented
Software, Addison Wesley

CT070-3-3 - Design Patterns Bridge Slide 17 (of 17)

You might also like