Design Pattern

Facade Design Pattern – Structural Patterns in Java

Facade Design Pattern is nothing but it simply interface of interfaces to simplify interactions between the client code and subsystem classes. This design comes under the GOF Structural Design Pattern. Facade provides clients with access to the system but conceals the working of the system and its complexities. The pattern creates one class consisting of user functions and delegates provide calling facilities to the classes belonging to the systems.

Facade Design Pattern

According to the Gang of Four:

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

UML Class Diagram for Facade Design Pattern

There are following classes and objects participating in this pattern are:


Facade (BankingServiceFacade)

  • This is a Facade interface knows which subsystem classes are responsible for a request. This interface is responsible to delegate client requests to appropriate subsystem objects.

Subsystem classes (AccountService, TransferService, PaymentService)

  • These interfaces are actually subsystem functionality functionalities of Banking Process System application. These are responsible to handle process assigned by the Facade object. No interfaces in this category has reference of Facade object, even they don’t have implementation details of Facade. These are totally independent from Facade object.

Pros of the Facade Pattern

There are following pros of using this facade pattern.

  • Facade pattern reduces complexities for client to interact with subsystems.
  • This pattern consolidates all the business services as single interface to make more understandable.
  • It reduces dependencies of client code on the inner workings of a system.

Cons of the Facade Pattern

There are following cons of using this facade pattern.

  • It leads to a larger API to maintain
  • It hides the important information in useless information

Applicability

Suppose you are designing a system, this system has very large number of independent classes and also has set of services to be implementing. This system is going to be very complex, Facade pattern comes into picture and reduce complexities of the larger system and simplifies interactions of the client code with the set of classes of subsystem of the large complex system.

Sample Implementation of the Facade Design Pattern

Suppose you want to develop an bank enterprise application with the large number services some of them as AccountService for getting the Account by accountId, PaymentService for payment gateway service and TransferService is using for actually amount transfer from one account to another account. A client code of the application interacts with these services for a amount transfer from one account to another account. This is how different clients interact with the amount transfer process of the bank system as below:

This interface is know as Facade interface, it is based the Facade pattern, it is a simple way to interact with the subsystems.

Step 1: Create an interface.

Account.java

/**
 * 
 */
package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public interface Account {
	
	void accountType();
}

Step 2: Create concrete classes implementing the same interface.

SavingAccount.java

/**
 * 
 */
package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public class SavingAccount implements Account {

	@Override
	public void accountType() {
		System.out.println("SAVING ACCOUNT");
	}

}

Step 3: Create a facade interface.

BankingServiceFacade.java

package com.doj.patterns.structural.facade;

public interface BankingServiceFacade {
	void moneyTransfer();
}

Step 4: Create a facade class.

BankingServiceFacadeImpl.java

package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public class BankingServiceFacadeImpl implements BankingServiceFacade{

	@Override
	public void moneyTransfer() {
		if(PaymentService.doPayment()){
			Account fromAccount = AccountService.getAccount("1");
			Account toAccount   = AccountService.getAccount("2");
			TransferService.transfer(1000, fromAccount, toAccount);
		}
	}
}

Step 5: Create a service classes.

AccountService.java

package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public class AccountService {

	public static Account getAccount(String accountId) {
		return new SavingAccount();
	}
}

PaymentService.java

package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public class PaymentService {
	public static boolean doPayment(){
		return true;
	}
}

TransferService.java

package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public class TransferService {

	public static void transfer(int amount, Account fromAccount, Account toAccount) {
		System.out.println("Transfering Money");
	}
}

Step 6: Use the facade to use various type of services.

FacadePatternDemo.java

package com.doj.patterns.structural.facade;

/**
 * @author Dinesh.Rajput
 *
 */
public class FacadePatternDemo{
	public static void main(String[] args) {
		BankingServiceFacade serviceFacade = new BankingServiceFacadeImpl();
		serviceFacade.moneyTransfer();
	}

}

Step 7: Let’s run this demo class and verify the output.

Transfering Money

Facade Vs Mediator Design Pattern

Mediator design pattern may look very similar to facade design pattern in terms of abstraction. Mediator abstracts the functionality of the subsystems in this way it is similar to the facade pattern. In the implementation of mediator pattern, subsystem or peers components are aware of the mediator and that interact with it. In the case of facade pattern, subsystems are not aware of the existence of facade. Only facade talks to the subsystems.

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago