Scala: Facade Design Pattern

Hi Everyone,

In our previous blogs, we have discussed several Structural design patterns i.e., Decorator design pattern, Adapter design pattern and Proxy design pattern.
In this blog, we will be discussing Facade Design pattern and will try to implement it in Scala.

Problem Statement:

We recently had a tour to Udaipur from our office and to organize it, we inquired a travel agency and asked them to share the quotations. Their quotation was dependent on several factors like category(Group, Family, Honeymoon) and package (Silver, Gold, Platinum).

On the basis of the choice of all these factors, the Booking process was initiated for which they used the Tour and Travel Management application.

This application includes several subsystems, few of them are :

  1. Ticket Booking
  2. Hotel Booking
  3. Booking a vehicle for sightseeing
  4. Booking Tour Guide

facade design pattern

To use these subsystems, our client can directly interact with it. But there is a problem with this approach, i.e., if there is any change in the subsystem like adding/updating/ deleting of a new Service in the subsystem, then we will also have to make changes in the client application, which is not a good approach.

Here comes the role of the Facade design pattern.

What is the Facade Design pattern?

A facade design pattern is a structural design pattern and is commonly used with object-oriented programming.

Facade Design Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

A facade design pattern is generally used by the developers when a system is very complex or difficult to understand or when the system has a large number of interdependent classes or probably when its source code is unavailable.

This pattern helps in hiding the complexities of the large system and providing a simple interface to the client.

Solution :

We can create a Facade class as an interface between the Client and our Subsystems. In that way, the Client will interact with the Facade and our Facade class will interact with the subsystems instead of the client directly interacting with the Subsystems.

facade design pattern 2

Example:

Client.scala

TourAndTravelFacade.scala

TicketBooking.scala

HotelBooking.scala

ShuttleBooking.scala

GuideBooking.scala

Output:

Hope you liked the blog. Thanks for reading!

References:

Written by 

Nancy jain is a software consultant with experience of more than 6 months. She likes to explore new technologies and trends in the IT world. Her hobbies include watching web series, writing and travelling. Nancy is familiar with programming languages such as Java, Scala, C, C++, HTML, Javascript and she is currently working on reactive technologies like Scala, DynamoDb, AkkaHttp.

Leave a Reply

Knoldus Pune Careers - Hiring Freshers

Get a head start on your career at Knoldus. Join us!