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 :
- Ticket Booking
- Hotel Booking
- Booking a vehicle for sightseeing
- Booking Tour Guide
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.
Example:
Client.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
TourAndTravelFacade.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
HotelBooking.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package subsystems | |
class HotelBooking { | |
def bookHotel(tourPackage : String, destination : String) : Unit = { | |
println("Hotel booked at " + destination+ " under package " + tourPackage) | |
} | |
} |
ShuttleBooking.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package subsystems | |
class ShuttleBooking { | |
def bookShuttle(tourPackage : String) : Unit = { | |
println("Shuttle is booked under package " + tourPackage) | |
} | |
} |
GuideBooking.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package subsystems | |
class GuideBooking { | |
def bookGuide(destination : String) : Unit = { | |
System.out.println("A guide is booked for sightseeing at " + destination) | |
} | |
} |
Output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Hope you liked the blog. Thanks for reading!
References:
- https://en.wikipedia.org/wiki/Facade_pattern
- http://www.genericclass.com/java/facade-design-pattern/
- https://www.scala-lang.org/old/sites/default/files/FrederikThesis.pdf