facade and adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · facade † pattern name:...

32
Facade and Adapter Comp-303 : Programming Techniques Lecture 19 Alexandre Denault Computer Science McGill University Winter 2004 March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 1

Upload: truongminh

Post on 10-Sep-2018

235 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Facade and Adapter

Comp-303 : Programming Techniques

Lecture 19

Alexandre DenaultComputer ScienceMcGill University

Winter 2004

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 1

Page 2: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Last lecture . . .

• Abstract Factories allow you to use families of objects in ageneric way.

• Factory methods allow you to choose the appropriate subclassat runtime.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 2

Page 3: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Typical Situation

• I’m building an inventory application with a database.

• The Client class uses the Database, Models and Elementsclasses.

• First, a Client object must open a connection to the Database.

• From the Database, it gets a Model.

• It then uses a Model to get an Element.

• Finally, it retrieves the much needed information from Element.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 3

Page 4: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Too many relations!

Client A

Client B

Database

Model

Element

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 4

Page 5: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Facade

• Pattern Name : Facade

• Classification : Structural

• Intent : Provide a unified interface to a set of interfaces in asubsystem. Facade defines a higher-level interface that makesthe subsystem easier to use.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 5

Page 6: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Motivation

• In our example, Client doesn’t really need to know aboutDatabase, Model or Element.

• It only needs to retrieve information from Element.

• Thus, we can build a Facade.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 6

Page 7: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Motivation (cont.)

Client A

Client B

Database

Model

Element

DatabaseFacade

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 7

Page 8: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Applicability

The Facade pattern should be used when . . .

• . . . you want to provide a simple interface to a complexsubsystem.

• . . . there are many dependencies between clients and theimplementation classes of an abstraction.

• . . . you want to layer your subsystems.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 8

Page 9: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Structure

Subsystem D

Subsystem C

Subsystem A

DatabaseFacade

Subsystem B

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 9

Page 10: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Participants

• Facade :

– knows which subsystem classes are responsible for a request.

– delegates client requests to appropriate subsystem objects.

• Subsystem :

– implement subsystem functionality.

– handle work assigned by the Facade object.

– have no knowledge of the Facade ( no reference to it ).

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 10

Page 11: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Collaborations

• Clients communicate with the subsystem by sending therequests to Facade.

• Facade forwards the request to the appropriate subsystem.

• Thought Facade doesn’t do any the of the work, it might needto translate the request.

• Clients that use Facade have no knowledge of the subsystem.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 11

Page 12: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Consequences

• It shields client from the many objects of the subsystemcomponent. Thus, the subsystem is easier to use.

• It promotes weak coupling between the client and thesubsystem.

• It doesn’t prevent the client from using the subsystem.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 12

Page 13: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Implementation

• Facade by itself is pretty simple to implement.

• You must decide if the subsystem should be made of private orpublic classes. This depends on if you want the client to beable to use the subsystem directly.

• You can make the Facade an abstract class. Thus, you cansubclass it and provide different Facades to different subsystem.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 13

Page 14: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Known Uses

• If a Facade is well built, the user shouldn’t even know it’s aFacade.

• Some subsystem in Java are good candidates for Facades(JDBC).

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 14

Page 15: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Related Patterns

• Abstract Factory

• Singleton

• Mediator

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 15

Page 16: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Something different

• The Facade is useful, but it doesn’t help you when you needpolymorphic behavior.

• For example, let’s think of a drawing program like PaintBrush.

• Implementing objects to draw basic primitives like Line andCircle is not too complicated.

• We can make those subclasses of an abstract class name Shape.

• However, drawing text is a little more complicated.

• Many external libraries are available to draw text.

• However, how do we preserve our object hierarchy with Shape?

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 16

Page 17: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Adapter

• Pattern Name : Adapter

• Classification : Structural

• Also known as : Wrapper

• Intent : Convert the interface of a class into another interfaceclients expect. Adapter lets classes work together that couldn’totherwise because of incompatible interfaces.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 17

Page 18: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Motivation

• We can create a class named TextShape which extends shape.

• This classes will use the external libraries to draw text.

• It can also provide additional functionalities not found in theexternal libraries .

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 18

Page 19: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Motivation (cont.)

TextLib

TextShape

refAdaptee: TextLib

Line

Shape

Circle

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 19

Page 20: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Applicability

The Adapter pattern should be used when . . .

• . . . you want to use an existing class, and its interface does notmatch the one you need.

• . . . you want to create a reusable class that cooperates withunrelated or unforeseen classes, that is, classes that don’tnecessarily have compatible interfaces.

• . . . you need to use several existing subclasses, but it’simpractical to adapt their interface by subclassing every one.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 20

Page 21: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Structure

Adaptee

specialRequest()

Adapter

refAdaptee: Adaptee

request()

Target

request()

Client

...

refAdaptee.specialRequest()

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 21

Page 22: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Participants

• Target : defines the domain-specific interface that Client uses.

• Client : collaborates with objects conforming to the Targetinterface.

• Adaptee : defines an existing interface that needs adapting.

• Adapter : adapts the interfaces of Adaptee to the Targetinterface.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 22

Page 23: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Collaborations

• Clients sends a request to the Adapter.

• In turn, the Adapter sends that request (maybe aftertransforming it) to the Adaptee.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 23

Page 24: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Consequences

• Allows you to use the Adaptee (with the help of the Adapter)as if it were part of your hierarchy.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 24

Page 25: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Implementation

• The implementation of an adaptor is fairly straightforward.

• A single Adapter can work with many Adaptees.

• Another variation of the Adapter pattern is possible when youhave multiple inheritance.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 25

Page 26: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Implementation (cont.)

Adaptee

specialRequest()

Adapter

request()

Target

request()

Client

...

specialRequest()

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 26

Page 27: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Known Uses

• The Java API already has a great number of Adapters built-in.

• One the most commonly used it the WindowAdapter class.

• Like Facade, when an Adapter is well built, the user shouldn’teven know he’s using an Adapter.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 27

Page 28: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Related Patterns

• Bridge

• Decorator

• Proxy

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 28

Page 29: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Facade vs Adapter

• Both design patterns are very similar.

• They are often used as wrappers for more complex functions.

• However, Adapter is used when our wrapper must respect aparticular interface and must support a polymorphic behavior.

• On the other hand, Facade is used when we want aeasier/simpler interface to work with.

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 29

Page 30: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Summary

• Facade allows us to hide a complex subsystem, thus reducecoupling and make that subsystem easier to use.

• Adapter allows us to change the interface of a class, thusallowing us to that class in our hieratic (polymorphic behavior).

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 30

Page 31: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

Tool of the day: SableVM

• SableVM is a portable bytecode interpreter written in C, andimplementing the Java virtual machine specification, secondedition.

• Its goals are to be reasonably small, fast, and efficient, as wellas providing a well-designed and robust platform forconducting research.

• SableVM is licensed under the GNU Lesser General PublicLicense (LGPL).

• It also makes use of a modified version of GNU Classpath.

• The initial development of SableVM was done as part of thePh.D. research project of Etienne Gagnon.

• More information on SableVM is available at:http://www.sablevm.org/

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 31

Page 32: Facade and Adapter - cs.mcgill.caadenau/teaching/cs303/lecture19.pdf · Facade † Pattern Name: Facade † Classiflcation: Structural † Intent: Provide a unifled interface to

References

• These slides are inspired (i.e. copied) from these three books.

– Design Patterns, Elements of Reusable Object-OrientedSoftware; Erich Gamma, Richard Helm, Ralph Johnson andJohn Vlissides; Addison Wesley; 1995

– Java Design Patterns, a Tutorial; James W. CooperAddison Wesly; 2000

– Design Patterns Explained, A new Perspective on ObjectOriented Design; Alan Shalloway, James R. Trott; AddisonWesley; 2002

March 23, 2004 Lecture 19 – Comp 303 : Facade and Adapter Page 32