Sie sind auf Seite 1von 21

Design patterns

Facade
Adapter
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.

15.03.08 Adam Siemion 2


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.
15.03.08 Adam Siemion 3
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.
15.03.08 Adam Siemion 4
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.

15.03.08 Adam Siemion 5


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.
15.03.08 Adam Siemion 6
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.

15.03.08 Adam Siemion 7


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.
15.03.08 Adam Siemion 8
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;
}
}

15.03.08 Adam Siemion 9


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.

15.03.08 Adam Siemion 10


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

15.03.08 Adam Siemion 11


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.

15.03.08 Adam Siemion 12


Structure I

A class adapter uses multiple inheritance to


adapt one interface to another

15.03.08 Adam Siemion 13


Structure II

 An object adapter relies on object


composition

15.03.08 Adam Siemion 14


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.

15.03.08 Adam Siemion 15


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.
15.03.08 Adam Siemion 16
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.

15.03.08 Adam Siemion 17


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.

15.03.08 Adam Siemion 18


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)

15.03.08 Adam Siemion 19


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.

15.03.08 Adam Siemion 20


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

15.03.08 Adam Siemion 21

Das könnte Ihnen auch gefallen