design pattern - facade pattern

13
Façade Pattern From Definition to implementation Mudasir Qazi - [email protected] 1 12-Dec-14

Upload: mudasir-qazi

Post on 07-Aug-2015

171 views

Category:

Engineering


2 download

TRANSCRIPT

Page 1: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 1

Façade Pattern

From Definition to implementation

12-Dec-14

Page 2: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 2

Contents / Agenda

• Definition• Structural Diagrams• Advantages and Usage• UML – Sequence Diagram• Implementation (step 1 to 5)

12-Dec-14

Page 3: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 3

Definition

• A Facade Pattern says that just "just provide a unified and simplified interface to a set of interfaces in a subsystem, therefore it hides the complexities of the subsystem from the client".• In other words, Facade Pattern describes a higher-level interface that

makes the sub-system easier to use.• Practically, every Abstract Factory is a type of Facade.• This pattern involves a single class which provides simplified methods

which are required by client and delegates calls to existing system classes methods.• It comes under “Structural Design Patterns” category.12-Dec-14

Page 4: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 4

Structural Diagram (1)

12-Dec-14

Page 5: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 5

Structural Diagram (2)

12-Dec-14

Page 6: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 6

Advantages and Usage

• Advantages1. It shields the clients from the complexities of the sub-system components.2. It promotes loose coupling between subsystems and its clients.

• Usage1. When you want to provide simple interface to a complex sub-system.2. When several dependencies exist between clients and the implementation

classes of an abstraction.3. Login in complex organizations like google, Microsoft’s accounts.

12-Dec-14

Page 7: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 7

UML – Sequence Diagram

Note the differences between ‘Before’ and ‘after’ event sequence. In first case, each time client need to interact with System it has to interact with 3 separate sub systems.In Second case, client just interact with Façade and it manages all internal affairs.

12-Dec-14

Page 8: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 8

Implementation (C# and Java)

• We are going to implement following system class diagram.

Each time we need to create a ‘Shape’, we just interact with ShapeMaker (Façade) and it will manage the internal complexities.

12-Dec-14

Page 9: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 9

Step 1

• Create an interface to enforce common method.In our example it would be Shape interface containing a method draw.

12-Dec-14

Page 10: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 10

Step 2

• Define concrete classes and implement interface.

12-Dec-14

Page 11: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 11

Step 3

Façade class acts as a wrapper class which contains all sub systems. In our example it will contain Circle, Rectangle and Square type of objects.Then you can have different necessary methods.

12-Dec-14

Page 12: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 12

Step 4

• Use Façade class to use sub systems (to draw different shapes).

12-Dec-14

Page 13: Design pattern - Facade Pattern

Mudasir Qazi - [email protected] 13

Step 5 - output

12-Dec-14