You are on page 1of 16

URSS Training:

Facade Design
Pattern
16.10.2007
Balint Eniko

Cluj- Napoca, 2011

internal / confidential

USS- EF

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

Definition

Provide a unified interface to a set of interfaces


in a subsystem.
Defines a higher-level interface that makes the
subsystem easier to use.

Cluj- Napoca, 2011

internal / confidential

USS- EF

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

Motivation

Structuring a system into subsystems helps


reduce complexity
The interface exposed by the classes in a
subsystem or set of subsystems can become
quite complex
One solution: to introduce a facade object that
provides a single, simplified interface

Cluj- Napoca, 2011

internal / confidential

USS- EF

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

Applicability

Use the Facade pattern:

To provide a simple interface to a complex


subsystem.
This interface is good enough for most clients;
more sophisticated clients can look beyond
the facade.
To decouple the classes of the subsystem
from its clients and other subsystems
subsystem independence and portability
Cluj- Napoca, 2011

internal / confidential

USS- EF

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

Benefits and Drawbacks

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 change the classes
without affecting the clients
It does not prevent sophisticated clients from
accessing the underlying classes!
It does not add any functionality, it just simplifies
interfaces!
Cluj- Napoca, 2011

internal / confidential

USS- EF

10

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

11

Facade Class Diagram

Cluj- Napoca, 2011

internal / confidential

USS- EF

12

Agenda

Definition
Motivation
Applicability
Benefits and Drawbacks
Faade Class Diagram
Example

Cluj- Napoca, 2011

internal / confidential

USS- EF

13

Example

Hiding the parts of a complicated calendar API


behind a more user friendly faade.
Classes:
UserfriendlyDate.java (pattern)
FacadePattern.java (client)

Cluj- Napoca, 2011

internal / confidential

USS- EF

14

Example - UserfriendlyDate

class UserfriendlyDate {
Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd");
public UserfriendlyDate (String isodate_ymd) throws ParseException {
Date date = sdf.parse(isodate_ymd);
cal.setTime(date);
}
public void addDays (int days) {
cal.add (Calendar. DAY_OF_MONTH, days);
}
public String toString() {
return sdf.format(cal.getTime());
}
}

Cluj- Napoca, 2011

internal / confidential

USS- EF

15

Example - Client

class FacadePatternClient {
public static void main(String[] args) throws ParseException {
UserfriendlyDate d = new UserfriendlyDate("1980-08-20");
System.out.println ("Date: " + d.toString());
d.addDays(20);
System.out.println ("20 days after: " +
d.toString());
}
}

Example output:
Date: 1980-08-20
20 days after: 1980-09-09

Cluj- Napoca, 2011

internal / confidential

USS- EF

16

You might also like