Design Pattern

How to implement Facade Design Pattern using Java?

Rohan Ravindra Kadam
Javarevisited
Published in
5 min readNov 17, 2021

--

Hello👋 Its Rohan Kadam😊

Maybe you are a newbie to coding👩‍💻 or programming, maybe experienced👴 or maybe a FrontEnd Developer or BackEnd Developer you all have may have come across the word Design Pattern as a principle or as a concept. The design pattern is a way or approach to writing code or developing an application. In this article, we try to answer certain questions What is Design Pattern? Why implement a design pattern? How to implement a Design pattern (Facade)?

How to Implement Facade Design Pattern?

⚡What is Design Pattern?

Design patterns are the solutions to commonly occurring problems in software design. They are like pre-fabricated blueprints that we can customize to solve a recurring design problem in your code.

They are not pieces of code or libraries that can be imported and used in the development of software or solving a particular problem. We can follow the pattern details and implement a solution that suits the realities of your program.

We often confused ourselves between design patterns and algorithms. While an algorithm always defines a clear set of actions that can achieve some goal, a pattern is a more upper-level description of a solution.

An analogy to an algorithm is a cooking dish: both have clear steps to achieve a goal. They’re not libraries or modules; they’re guidelines you integrate into the core of your designs, giving you a leg up in creating flexible and maintainable object-oriented systems.

⚡Why implement a Design Pattern?

We have encountered design patterns only in a nutshell, they’re general object-oriented solutions that you can use in your designs. Crafted by experienced 👴object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. So, if design patterns still aren’t part of your development toolbelt, here are five reasons you should add them.

1 → Make our life easier by not reinventing the wheel

2 → Improve our object-oriented Skills

3 → Recognize Patterns in libraries and languages

4 → Use the power of a shared vocabulary

5 → Find Truth and beauty

⚡How to implement a Facade Design Pattern?

Before jumping directly into the implementation of the design pattern we need to answer certain questions such as What is a Facade design pattern? Why incorporate the design pattern? and finally How to implement a Facade Design Pattern?

1 →What is Facade Design Pattern?

The Facade design pattern is part of the Structural Design Pattern Family, which provides a simplified interface to a library, a framework, or any other complex set of classes. A real-life example will be, Imagine you have run a big delivery business, as a manager you enter only package info that is package size and weight lets code decide best modes of transportation. For example the input screen act as Face or Facade to process behind it. That’s how the Facade Design Pattern works in real life.

Definition:
A Facade Design Pattern is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

2 → Why implement Facade Design Pattern?

  1. Facade allows us to follow the Design Principle (SOLID).
  2. It fosters isolating the code from the complexity of the subsystem.
  3. It allows us to introduce new code without affecting the existing code structure like Plug and Play Model

3 → How to Implement Facade Design Pattern?

In section, we try to implement Factory Design Pattern using Java. For example, we use the delivery manager example for selecting the mode of transportation.

Step 1 → What input should the delivery manager give?

As a Delivery Manager, he/she should give info about the package like size and weight, in our example, we are using PackageInfo to the job.

PackageInfo.class

Step 2 → What will be various modes for making a delivery?

The various modes of delivering packages are car🚗, bicycle🏍 and truck🚚. We have created three-class defining the modes for delivery.

PackageDelivery

We have created subclasses that are implemented by the PackageDelivery interface.

1 →Delivery by bicycle🏍

DeliveryByBicycle.class

2 →Delivery by Car🚗

DeliveryByCar.class

3 →Delivery by Truck🚚

DeliveryByTruck.class

Delivery manager class to manage the transportation mode.

Delivery Manager Class

Step 3→ How to test the Facade Design Pattern?

To test our Facade Design Pattern we are giving inputs package size and weight.

Based on input the code decides the best mode of transportation

Step 4→What will be the Output for Facade Pattern?

Below console output helps to understand how the Manager(Facade Class) is selecting the mode of transportation based on Package Info

⚡Where to find the Codebase for Facade Design Pattern?

On Github

⚡Conclusion:-

In the article, we tried to answer some questions related to Design Pattern and especially Facade Design Pattern how it helps developers around the world to write better code and build faster applications. Facade Design Pattern allows us to implement the SOLID principle effectively. Best examples of Facade Design Pattern will be Maven dependency or NPM modules, libraries etc.

Please do share and like💖 if you find the article useful. Follow me on medium Rohan Ravindra Kadam and on Twitter at rohankadam25

📚Bibliography:-

Thank You, Viewers — Rohan Kadam

--

--