0% found this document useful (0 votes)
44 views29 pages

Façade Pattern in Software Design

The document describes the facade pattern, which provides a unified interface to a set of interfaces in a subsystem. The facade pattern involves encapsulating the subsystem and exposing a higher-level interface. This reduces coupling between the subsystem and its clients, and simplifies client code. The document uses examples of a compiler subsystem and home theater system to illustrate refactoring initial designs using the facade pattern to address problems like duplicate code and changes breaking clients.

Uploaded by

王小狗
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views29 pages

Façade Pattern in Software Design

The document describes the facade pattern, which provides a unified interface to a set of interfaces in a subsystem. The facade pattern involves encapsulating the subsystem and exposing a higher-level interface. This reduces coupling between the subsystem and its clients, and simplifies client code. The document uses examples of a compiler subsystem and home theater system to illustrate refactoring initial designs using the facade pattern to address problems like duplicate code and changes breaking clients.

Uploaded by

王小狗
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Façade Pattern

Shin-Jie Lee (李信杰)


Associate Professor
Computer and Network Center
Department of CSIE
National Cheng Kung University
Design Aspect of Facade

Interface to a subsystem

2
Outline
Requirements Statement
Initial Design and Its Problems
Design Process
Refactored Design after Design Process
Another Example
Recurrent Problems
Intent
Façade Pattern Structure
Homework

3
A Programming
Environment

4
Requirements Statement1
A compiler subsystem contains classes such as
Scanner, Parser, ProgramNode, and
BytecodeStream.

Compiler Parser
Subsystem
BytecodeStream
Scanner

ProgramNode

5
Requirements Statement2
The client classes need to use Scanner, Parser,
ProgramNode, and BytecodeStream to compile
some code.
Client2
Client1 compile()
Parser parser = new Parser();
compile() Scanner scanner = new Scanner();
ProgramNode programNode = new ProgramNode();
BytecodeStream bytecodeStream = new BytecodeStream();
…to compile some code

Parser

Scanner
BytecodeStream

ProgramNode

Compiler
Subsystem

6
Initial Design

Client2
Client1 compile()
Parser parser = new Parser();
compile() Scanner scanner = new Scanner();
ProgramNode programNode = new ProgramNode();
BytecodeStream bytecodeStream = new BytecodeStream();
…to compile some code

Parser

Scanner
BytecodeStream

ProgramNode

Compiler Subsystem

7
Problems with Initial Design

Problem1: When more and more clients want to


use this compiler subsystem, it will cause a lot Problem2: If compiler subsystem changes,
of duplicate code. all the clients will be modified.

Client2
Client1 compile()
Parser parser = new Parser();
compile() Scanner scanner = new Scanner();
ProgramNode programNode = new ProgramNode();
BytecodeStream bytecodeStream = new BytecodeStream();
…to compile some code

Parser

Scanner
BytecodeStream

ProgramNode

Compiler Subsystem

8
Design Process for Change

Design Principle: Encapsulate what


The code that changes has been varies.
encapsulated as a class?

Need abstraction?
Act-1: Encapsulate
No What Varies, methods No
and its corresponding
attributes Need composition?
Yes Yes

Act-2: Abstract Common Behaviors No


(with a same signature) into Interfaces
or Abstract Classes Yes

Design Principle: Program to an


interface, not an implementation.

Act-3: Compose or
Delegate Abstract Expose new interfaces?
Design Principle: Depend on
abstractions. Do not depend on Behaviors
concrete classes.

Yes No

9
Act-1: Encapsulate What Varies

Client1
compile()

Act-1.2: Encapsulate a method Compiler


compile()
into a concrete class
Client2
compile()

10
Act-3: Compose Abstract Behaviors

Client1 Client2

Compiler
Act-3.4: Delegate
compile()
behavior to a
method of a Parser
concrete class
Scanner
BytecodeStream

ProgramNode

Compiler Subsystem

11
Refactored Design after Design Process

Client1 Client2

Compiler
compile()

Parser

Scanner
BytecodeStream

ProgramNode

Compiler Subsystem

12
Home Theater

13
Requirements Statement1
A Home Theater consists of an amplifier, a DVD
player, a projector, a screen, a popcorn popper,
and theater lights.

Amplifier DVDPlayer Projector

Screen PopcornPopper TheaterLights

Home Theater

14
Requirements Statement2
A user can watch a movie through the following
process:
1. Turn on the popcorn popper
2. Start the popper popping
3. Dim the lights
4. Put the screen down
5. Turn the projector on
6. Turn the sound amplifier on
7. Turn the DVD player on
8. Start the DVD player playing

15
Initial Design

Client PopcornPopper popper = new PopcornPopper();


popper.on();
watchAMovie() popper.pop();

TheaterLights lights = new TheaterLights();


lights.dim();

Screen screen = new Screen();


Amplifier DVDPlayer Projector Screen.down();

on() on() on() Projector projector = new Projector();


play() projector.on();

Amplifier amplifier = new Amplifier();


Screen PopcornPopper TheaterLights amplifier.on();
down() on() dim() DVDPlayer player = new DVDPlayer();
pop() player.on();
player.play();
Home Theater

16
Problems with Initial Design

Client PopcornPopper popper = new PopcornPopper();


popper.on();
watchAMovie() popper.pop();

TheaterLights lights = new TheaterLights();


lights.dim();

Screen screen = new Screen();


Amplifier DVDPlayer Projector Screen.down();

on() on() on() Projector projector = new Projector();


play() projector.on();

Amplifier amplifier = new Amplifier();


Screen PopcornPopper TheaterLights amplifier.on();
down() on() dim() DVDPlayer player = new DVDPlayer();
pop() The clients outside the home theater system
Problem: know too
player.on();
Home Theater much about the components inside the system. In case the system
player.play();
is upgraded or changed, clients have to change for accommodation.

17
Design Process for Change

Design Principle: Encapsulate what


The code that changes has been varies.
encapsulated as a class?

Need abstraction?
Act-1: Encapsulate
No What Varies, methods No
and its corresponding
attributes Need composition?
Yes Yes

Act-2: Abstract Common Behaviors No


(with a same signature) into Interfaces
or Abstract Classes Yes

Design Principle: Program to an


interface, not an implementation.

Act-3: Compose or
Delegate Abstract Expose new interfaces?
Design Principle: Depend on
abstractions. Do not depend on Behaviors
concrete classes.

Yes No

18
Act-1: Encapsulate What Varies

PopcornPopper popper = new PopcornPopper();


popper.on();
popper.pop();
Client WatchAMovie
TheaterLights lights = new TheaterLights(); watchAMovie() watchAMovie()
lights.dim();

Screen screen = new Screen();


Screen.down(); Act-1.2: Encapsulate
Projector projector = new Projector(); a method into a
projector.on();
concrete class
Amplifier amplifier = new Amplifier();
amplifier.on();

DVDPlayer player = new DVDPlayer();


player.on();
player.play();

19
Act-3: Compose Behaviors

Act-3.4: Delegate behavior to


(a method of) a concrete class
WatchAMovie theater = new WatchAMovie();
theater.watchAMovie(); Act-3.2: Compose behaviors (multiple
Client methods) of a concrete class
watchAMovie()
WatchAMovie
watchAMovie()
popper.on();
popper.pop();
Amplifier DVDPlayer Projector lights.dim();
screen.down();
on() on() on() projector.on();
play() amplifier.on();
player.on();
player.play();
Screen PopcornPopper TheaterLights
down() on() on()
pop()
Home Theater

20
Refactored Design after Design Process

popper.on();
popper.pop();
WatchAMovie theater = new WatchAMovie(); lights.dim();
theater.watchAMovie(); screen.down();
Client projector.on();
amplifier.on();
watchAMovie() player.on();
WatchAMovie player.play();
watchAMovie()

Amplifier DVDPlayer Projector


on() on() on()
play()

Screen PopcornPopper TheaterLights


down() on() on()
pop()
Home Theater

21
Recurrent Problem
A common design goal is to minimize the
communication and dependencies between
subsystems.
 One way to achieve this goal is to introduce a façade
object that provides a single, simplified interface to the
more general facilities of a subsystem.

22
Intent
Provide a unified interface to a set of interfaces in
a subsystem. Façade defines a higher-level
interface that makes the subsystem easier to use.

23
Façade Structure1

Client

Façade
subsystem
classes

24
Façade Structure2

Client Façade subsystem class1 subsystem class2

1. Client instantiates a
Façade object. <<create>> 3. Façade has the idea of
communicating with
request() subsystem classes to
2. Client delegates the accomplish Client’s request.
request to Façade.

25
Façade Structure3

Instantiation Use Termination

Façade Client Client Don’t Care

subsystem
Don’t Care Façade Don’t Care
classes

26
Homework1: Requirements Statement1

 Opening Documents in Applications


 In order to open a text document, a text application
will:
• Check if the text document can be opened
• Create a text document object and hold a reference of the text
document object.
• Add the text document object to the Application.
• Read text document.

27
Homework1: Requirements Statement2

 Opening Documents in Applications


 There is another new requirement.
 Opening a spreadsheet document with a spreadsheet
application carries the same steps in the algorithm
(process) as the text document.
• Check if the spreadsheet document can be opened.
• Create a spreadsheet document object and hold a reference of
the spreadsheet document object.
• Add the spreadsheet document object to the Application.
• Read spreadsheet document.

28
Homework2: Requirements Statement

 Prepare Caffeine Beverages


 Please follow these recipes precisely when preparing
Starbuzz beverages
• Starbuzz Coffee Recipe
– Boil some water
– Brew coffee in boiling water
– Pour Coffee in cup
– Add sugar and milk
• Starbuzz Tea Recipe
– Boil some water
– Steep tea in boiling water
– Pour tea in cup
– Add lemon
29

You might also like