design patterns - the facade and adapter pattern

21
Design patterns Facade Adapter

Upload: adam-siemion

Post on 13-Nov-2014

2.903 views

Category:

Documents


4 download

DESCRIPTION

"Design patterns - the facade and adapter pattern", presentation PJWSTK 2005

TRANSCRIPT

Page 1: Design patterns - the facade and adapter pattern

Design patterns

Facade

Adapter

Page 2: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 2

The Facade Pattern

Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

Page 3: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 3

Motivation

Structuring a system into subsystems helps reduce complexity.

Subsystems are groups of classes, or groups of classes and other subsystems.

The interface exposed by the classes in a subsystem or set of subsystems can become quite complex.

One way to reduce this complexity is to introduce a facade object that provides a single, simplified interface to the more general facilities of a subsystem.

Page 4: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 4

Applicability 1

To provide a simple interface to a complex subsystem. A facade can provide a simple default view of the subsystem that is good enough for most clients. Only clients needing more customizability will need to look beyond the facade.

Introduce a facade to decouple the subsystem from clients and other subsystems, thereby promoting subsystem independence and portability.

Page 5: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 5

Applicability 2

To layer your subsystems. Use a facade to define an entry point to each subsystem level. If subsystems are dependent, then you can simplify the dependencies between them by making them communicate with each other solely through their facades.

Page 6: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 6

Collaborations

Clients communicate with the subsystem by sending requests to Facade, which forwards them to the appropriate subsystem object(s). Although the subsystem objects perform the actual work, the facade may have to do work of its own to translate its interface to subsystem interfaces.

Clients that use the facade don't have to access its subsystem objects directly.

Page 7: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 7

Benefits 1

It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use.

It hides the implementation of the subsystem from clients, making the subsystem easier to use.

It promotes weak coupling between the subsystem and its clients. This allows you to change the classes the comprise the subsystem without affecting the clients.

Page 8: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 8

Benefits 2

It reduces compilation dependencies in large software systems.

It simplifies porting systems to other platforms, because it's less likely that building one subsystem requires building all others.

It does not prevent sophisticated clients form accessing the underlying classes !

Note that Facade does not add any functionality, it just simplifies interfaces.

Page 9: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 9

Source code

public class FacadeCuppaMaker { public TeaCup makeACuppa() {

TeaCup cup = new TeaCup(); TeaBag teaBag = new TeaBag();

Water water = new Water();

cup.addTeaBag(teaBag); water.boilWater();

cup.addWater(water); cup.steepTeaBag();

return cup; }

}

Page 10: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 10

The Adapter Pattern

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Also called wrapper.

Page 11: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 11

Two types of Adapter Pattern

Object Adapter pattern— relies on one object (the adapting object) containing another (the adapted object).

Class Adapter pattern— implemented with multiple inheritance

Page 12: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 12

Motivation

You want to use a subroutine or a method that someone else has written because it performs some function that you need.

You cannot incorporate the routine directly into your program.

The interface or the way of calling the code is not exactly equivalent to the way that its related objects need to use it.

Page 13: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 13

Structure I

A class adapter uses multiple inheritance to adapt one interface to another

Page 14: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 14

Structure II

An object adapter relies on object composition

Page 15: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 15

Participants

Target defines the domain-specific interface that Client uses.

Client collaborates with objects conforming to the Target

interface.

Adaptee defines an existing interface that needs adapting.

Adapter adapts the interface of Adaptee to the Target interface.

Page 16: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 16

Applicability

You want to use an existing class, and its interface does not match the one you need.

You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.

(object adapter only) You need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one.

Page 17: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 17

Benefits

Allows the client objects to deal with all these objects in the same way—freed from having to pay attention to their differences.

Also enables to add different kinds of objects in the future without having to change the clients code.

Page 18: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 18

Adapter Summary

The Adapter pattern is a very useful pattern that converts the interface of a class (or classes) into another interface, which we need the class to have. It is implemented by creating a new class with the desired interface and then wrapping the original class methods to effectively contain the adapted object.

Page 19: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 19

Differences and similarities between Facade & Adapter

In both the Facade and Adapter pattern we have preexisting classes.

In the Facade, however, we do not have an interface we must design to, as we do in the Adapter pattern.

We are not interested in polymorphic behavior in the Facade, while in the Adapter, We probably am. (There are times when we just need to design to a particular API and therefore must use an Adapter)

Page 20: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 20

Differences and similarities between Facade & Adapter

In the case of the Facade pattern, the motivation is to simplify the interface. With the Adapter, while simpler is better,We are trying to design to an existing interface and cannot simplify things even if a simpler interface were otherwise possible.

Bottom line: A Facade simplifies an interface while an Adapter converts the interface into a preexisting interface.

Page 21: Design patterns - the facade and adapter pattern

8.04.23 Adam Siemion 21

Bibliography

Design Patterns Explained: A New Perspective on Object-Oriented Design Alan Shalloway, James R. Trott

Design Patterns Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

http://c2.com/cgi/wiki?FacadePattern http://c2.com/cgi/wiki?AdapterPattern