0% found this document useful (0 votes)
9 views

SWE 302-lect06- Part II- Example

Uploaded by

doaagheidan123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SWE 302-lect06- Part II- Example

Uploaded by

doaagheidan123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Software Design & Architecture:

Design Patterns: Façade Design Pattern

A Simple Interface to a Complex System

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 1


Pattern Classification

 Creational

 Structural

 Behavioral

 Structural patterns are concerned with how classes and


objects are composed to form larger structures.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 2


Definition

 Provide a unified interface to a set of interfaces


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

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 3


Facade Pattern:
Why and What?
 Subsystems often get complex as they evolve.
 Need to provide a simple interface to many,
often small, classes. But not necessarily to ALL
classes of the subsystem.
 Façade provides a simple default view good
enough for most clients.
 Facade decouples a subsystem from its clients.
 A façade can be a single entry point to each
subsystem level.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 4


Motivation
 Structuring a system into subsystems helps reduce
complexity.
 A common design goal is to minimize the
communication and dependencies between
subsystems.
 Use a facade object to provide a single, simplified
interface to the more general facilities of a
subsystem.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 5


Real World Example

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 6


Facade Pattern: Problem & Solution

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 7


Facade Pattern: Problem & Solution

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 8


Façade: Example - Home Sweet
Home Theater

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 9


Watching a Movie the Hard Way!

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. Set the projector input to DVD
7. Put the projector on wide-screen mode
8. Turn the sound amplifier on
9. Set the amplifier to DVD input
10. Set the amplifier to surround sound
11. Set the amplifier volume to medium (5)
12. Turn the DVD player on
13. Start the DVD player playing.
14. Whew!

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 10


Façade!

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 11


Applicability

 Use the Facade pattern when


 you want to provide a simple interface to a complex
subsystem.

 there are many dependencies between clients and


the implementation classes of an abstraction.

 you want to layer your subsystems.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 12


Structure

Facade
Subsystem classes

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 13


Participants

 Façade
• knows which subsystem classes are responsible
for a request.
• delegates client requests to appropriate subsystem
objects.
 subsystem classes
• implement subsystem functionality.
• handle work assigned by the Facade object.
• have no knowledge of the facade; that is, they keep
no references to it.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 14


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.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 15


Consequences

 The Façade Patterns offers the following benefits:


 It promotes weak coupling between the
subsystem and its clients.
 It doesn’t prevent applications form using
subsystem classes if they need to. Thus you can
choose between ease of use and generality
 It shields client from subsystem components,
thereby reducing the number of objects that
clients deal with and making the subsystem
easier to use.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 16


Implementation

 Consider the following issues when


implementing a facade:

1. Reducing client-subsystem coupling.


2. Public versus private subsystem classes.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 17


The Façade Pattern: Structural
Code

**/
* Test driver for the pattern.
/*
public class Test {
public static void main( String arg[] ) {
Facade facade = new Facade();
facade.go();
{
{

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 18


The Façade Pattern: Structural
Code
**/
* Implement subsystem functionality. Handle work assigned by the Facade object.
* Have no knowledge of the facade; that is, they keep no references to it.
/*
public class Subsystem1 {
public void doWork(){
{
{
public class Subsystem2 {
public void doWork(){
{
{
public class Subsystem3 {
public void doWork(){
}
}
SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 19
The Façade Pattern: Structural
Code
**/
* Knows which subsystem classes are responsible for a request.
* Delegates clients requests to appropriate subsystem objects.
/*
public class Facade {
private Subsystem1 sub1 = new Subsystem1();
private Subsystem2 sub2 = new Subsystem2();
private Subsystem3 sub3 = new Subsystem3();
public void go() {
sub1.doWork();
sub2.doWork();
sub3.doWork();
{
{

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 20


Façade – the System Level

 General patterns for Constructing subsystems.


 Each subsystem is represented by a façade
interface.
 Inner details are encapsulated.
SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 21
Related Patterns

 Abstract Factory.

 Mediator

 Singletons.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 22


Summary

 When you need to simplify and unify a large


interface or a complex set of interfaces, use a
façade.
 A façade decouples the client from a complex
subsystem.
 Implementing a façade requires that we compose
the façade with its subsystem and use delegation to
perform the work of the façade.
 You can implement more than one façade for a
subsystem.
 A façade “wraps” a set of objects to simplify!

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 23


Application of Façade Design Pattern
on My Project
(Survey Creator & Analyzer)
 First, we represents the Survey Creator And
Analyzer subsystem without using Façade
class. And we see how the client interacts with
the subsystem to make a survey.
 Secondly, we represents the Survey Creator
And Analyzer subsystem by using Façade
class. And we see how the client interacts with
the subsystem to make a survey.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 24


Application of Façade Design Pattern on My Project
(Survey Creator & Analyzer)

Here are a lists of operations a client should perform to completes the


desired task (make a survey). These operations belongs to different
classes within the survey Creator and analyzer Subsystem.
 Place survey order.
 Approve survey.
 Create survey.
 Specify Question types
 Inter Survey questions
 Inter Survey Answers.
 Specify survey points.
 Notify the user to participate
 Check User Participation.
 Analyze the completed survey.
SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 25
Survey Creator
And Analyzer
without Using
Façade Class

In order to completes
his task, client should
accesses many
operations in different
classes within the
subsystem.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 26


Survey Creator And
Analyzer by Using
Façade Class

The client Sends his


request to the facade
class and the façade
interacts with other
subsystem classes to
fulfills the client’s
request.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 27


Thank You For
Listening.

SWE 302 Software Design and Architecture. Lecture 6 Part II Slide 28

You might also like