training design patterns facade
DESCRIPTION
Learn design patternsTRANSCRIPT
USS- EFCluj- Napoca, 2011
internal / confidential 1
Balint Eniko
Facade Design
Pattern
URSS Training:
16.10.2007
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example
2
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example
3
USS- EFCluj- Napoca, 2011
internal / confidential
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.
4
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example
5
USS- EFCluj- Napoca, 2011
internal / confidential
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
6
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example
7
USS- EFCluj- Napoca, 2011
internal / confidential
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
8
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example
9
USS- EFCluj- Napoca, 2011
internal / confidential
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!
10
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Facade Class Diagram
Example
11
USS- EFCluj- Napoca, 2011
internal / confidential
Facade Class Diagram
12
USS- EFCluj- Napoca, 2011
internal / confidential
Agenda
Definition
Motivation
Applicability
Benefits and Drawbacks
Façade Class Diagram
Example
13
USS- EFCluj- Napoca, 2011
internal / confidential
Example
▶ Hiding the parts of a complicated calendar API behind a more user friendly façade.
▶ Classes: UserfriendlyDate.java (pattern)
FacadePattern.java (client)
14
USS- EFCluj- Napoca, 2011
internal / confidential
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());}
}
15
USS- EFCluj- Napoca, 2011
internal / confidential
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
16