Scala: Facade Design Pattern

Reading Time: 2 minutes

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

package app
import facade.TourAndTravelFacade
object Client extends App
{
val facade = new TourAndTravelFacade
val tourPackage = "Platinum"
val destination = "Udaipur"
val departure = "Delhi"
facade.bookHolidayPackage(tourPackage, destination, departure)
}

view raw
Client.scala
hosted with ❤ by GitHub

TourAndTravelFacade.scala

package facade
import subsystems.{ShuttleBooking, GuideBooking, HotelBooking, TicketBooking}
class TourAndTravelFacade
{
val ticketBooking = new TicketBooking
val hotelBooking = new HotelBooking
val cabBooking = new ShuttleBooking
val guideBooking = new GuideBooking
def bookHolidayPackage(tourPackage : String, destination : String, departure : String) : Boolean =
{
ticketBooking.bookFlight(tourPackage, destination, departure)
hotelBooking.bookHotel(tourPackage, destination)
cabBooking.bookShuttle(tourPackage)
guideBooking.bookGuide(destination)
true
}
}

TicketBooking.scala

package subsystems
class TicketBooking {
def bookFlight(tourPackage : String, destination : String, departure : String) : Unit = {
println("Round Trip Flight tickets for " + destination + " & " + departure + " is booked under package " + tourPackage)
}
}

view raw
TicketBooking.scala
hosted with ❤ by GitHub

HotelBooking.scala

package subsystems
class HotelBooking {
def bookHotel(tourPackage : String, destination : String) : Unit = {
println("Hotel booked at " + destination+ " under package " + tourPackage)
}
}

view raw
HotelBooking.scala
hosted with ❤ by GitHub

ShuttleBooking.scala

package subsystems
class ShuttleBooking {
def bookShuttle(tourPackage : String) : Unit = {
println("Shuttle is booked under package " + tourPackage)
}
}

view raw
ShuttleBooking.scala
hosted with ❤ by GitHub

GuideBooking.scala

package subsystems
class GuideBooking {
def bookGuide(destination : String) : Unit = {
System.out.println("A guide is booked for sightseeing at " + destination)
}
}

view raw
GuideBooking.scala
hosted with ❤ by GitHub

Output:

Round Trip Flight tickets for Udaipur & Delhi is booked under package Platinum
Hotel booked at Udaipur under package Platinum
Shuttle is booked under package Platinum
A guide is booked for sightseeing at Udaipur

view raw
FacadeOutput
hosted with ❤ by GitHub

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.