You are on page 1of 16

6.

Facade

IOE 321 SDP 1


Intent
• It is a structural design pattern that provides a
simplified interface to a library, a framework, or any
other complex set of classes.

• Facade Pattern describes a


higher-level interface that makes
the sub-system easier to use.

• Practically, every Abstract


Factory is a type of Facade

IOE 321 SDP 2


Real-World Analogy

• When you call a shop to place a phone order, an operator


is your facade to all services and departments of the shop.
• The operator provides you with a simple voice interface to
the ordering system, payment gateways, and various
delivery services.

IOE 321 SDP 3


Real-World Analogy

• Another example: Start-up


of a computer
• When computer starts, it
involves the work of CPU,
memory, Hard drive etc..
• To make it easy for users, we
can add a façade which wraps
the complexity of the task and
provide one simple interface

IOE 321 SDP 4


General Structure
• Facade provides convenient access
to a particular part of the
subsystem’s functionality.
• It knows where to direct the client’s
request and how to operate all the
subsystems

• Additional Facade class can be


created to prevent polluting a
single facade with unrelated
features that might make it yet
another complex structure.
Additional facades can be used by
both clients and other facades.
IOE 321 SDP 5
General Structure
• Complex Subsystem consists of
dozens of various objects.
• To make them all do something
meaningful, subsystem’s
implementation, such as initializing
objects in the correct order and
supplying them with data in the proper
format.
• Subsystem classes aren’t aware of the
facade’s existence.

• Client uses the facade instead of calling the


subsystem objects directly.

IOE 321 SDP 6


Problem
• Hotel - lot of restaurants {e.g., Veg restaurants, Non-Veg restaurants,
and Veg/Non-Both restaurants}.
• Client à wants access to different menus of different restaurants. –
Don’t Know!!

• Client access to a hotel keeper à knows hotel well.


• Whichever menu you want, tell hotel keeper - he takes it out of the
respective restaurants and hands it you.
Hotel keeper acts as the facade, as he hides the complexities of the hotel.
Solution

IOE 321 SDP 8


Implementation
• HotelInterface.java

• NonVegRestaurant.java

IOE 321 SDP 9


• VegRestaurant.java

• VegNonBothRestaurant.java

IOE 321 SDP 10


Now let’s consider the facade,
• HotelKeeper.java

• HotelKeeperImple
mentation.java

IOE 321 SDP 11


• Client.java

IOE 321 SDP 12


Applicability
• Use the Facade pattern when you need to have simple
interface to a complex subsystem
• Use the Facade when you want to structure a subsystem
into layers

IOE 321 SDP 13


Pros and Cons
Pros:
• Isolate your code from the complexity of a subsystem.
• It shields the clients from the complexities of the sub-
system components.
• It promotes loose coupling between subsystems and
its clients.
Cons:
• A facade can become a god object coupled to all
classes of an app.

IOE 321 SDP 14


MID II - Syllabus
Structural Design Pattern:
1. Adapter
2. Bridge
3. Decorator
4. Proxy
5. Composite
6. Facade

IOE 321 SDP 15


Thank You

IOE 321 SDP 16

You might also like