About Mainak Goswami

Mainak Goswami is an experienced Technology Consultant specializing in JEE, Web Development and Open source technologies. He is currently based out of United Kingdom. He is a technology enthusiast trying to explore the latest in the world of technology. His current area of interest is Mobility, NoSQL and Cloud computing. In past time he loves blogging on his website Idiotechie.

Facade Design Pattern – Design standpoint

In our previous article we have described about the Adapter Design Pattern. In today’s article we are going to show about another such Gang of Four Structural patterns. As the name suggests structural pattern is used to form a larger object structure from many different objects. Façade pattern is one such pattern which provides a simplified interface to a set of interfaces within a system and thus it hides the complexities of the subsystem from the client.

When to use Façade Pattern?

Layering: Facade pattern can be used in JEE applications for creating a layer to abstract and unify the related interfaces in the application. Use of a facade will define an entry point to each subsystem level and thus make them communicate only through their facades; this can simplify the dependencies between them.

Façade makes the API and libraries easier to use which is good for maintenance and readability. It can also collate and abstract various poorly designed APIs with a single simplified API.

It also reduces dependencies of the external code on the inner working of the libraries and thus providing flexibility.

Facade Design Pattern structure

In the above structure for Façade pattern the Façade class insulates the subsystem from the client. The client only interacts with the Façade class without knowing about the subsystem classes.

Example:

Let’s take an example of Order processing online website. The client has placed an order without having knowledge of how internal classes are functioning. Once the order is placed the façade class layer calls the methods of the subsystems like Inventory for inventory check and Payment for processing of the payment. After processing it returns the control to the client class with the confirmation about the order being processed.

Sequence Diagram:

Facade Design Sequence Diagram

Code Example:

Inventory.java -

public class Inventory {
	public String checkInventory(String OrderId) {
		return 'Inventory checked';
	}
}

Payment.java

public class Payment {
	public String deductPayment(String orderID) {
		return 'Payment deducted successfully';

	}
}

OrderFacade.java

public class OrderFacade {
	private Payment pymt = new Payment();
	private Inventory inventry = new Inventory();

	public void placeOrder(String orderId) {
		String step1 = inventry.checkInventory(orderId);
		String step2 = pymt.deductPayment(orderId);
		System.out
				.println('Following steps completed:' + step1 
						+ ' & ' + step2);
	}
}

Client.java

public class Client {
  public static void main(String args[]){
	  OrderFacade orderFacade = new OrderFacade();
	  orderFacade.placeOrder('OR123456');
	  System.out.println('Order processing completed');
  }
}

 
Benefits:

  • We can use the façade pattern to collate all the complex method calls and related code blocks and channelizes it through one single Façade class. In this way with respect to client there is only one single call. Even if we make changes to the subsystem packages / class and their logic there is no impact to the client call. In short this increases loose coupling.
  • It makes easier to use and maintain creating a more structured environment and reduces dependencies between libraries or other packages.

 
Drawbacks/Consequences:

  • One of the drawback is that the subsystem methods are connected to the Façade layer. If the structure of the subsystem changes then it will require subsequent change to the Façade layer and client methods.

 
Interesting points:

Façade pattern might be confused with mediator pattern. Mediator also abstracts the functionality of the subsystem in similar manner to façade. However there is a subtle difference between both these patterns. In case of Mediator pattern the subsystem is aware of the mediator, however in case of Façade the subsystem does not know anything about the façade. It’s a one way communication from Façade to subsystem.

Façade used in Java API

  • javax.servlet.http.HttpSession
  • javax.servlet.http.HttpServletRequest
  • javax.servlet.http.HttpServletResponse
  • javax.faces.context.ExternalContext

 
Reference: Façade Design Pattern – Design standpoint from our JCG partner Mainak Goswami at the Idiotechie blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Facade Design Pattern – Design standpoint"

  1. Stella says:

    what the program did you used for pics ? looks very nice, is it some kind of uml designes ?

  2. manoj says:

    What is Adaptor

    Adaptor is used to make tow parties talk with each otherwise could not talk as their communication channel is not compatible with each other

    For Example :

    Adaptor for a mobile charger : Mobile can not be directly plugged in to electric socket to charge it . It between it requires an adaptor to to get connected. Adaptor converts 220V from electric socket to 9V what Mobile is made to accept .

    An interpreter between two people talking two different languages unknown to each other. Interpreter acts as adaptor here.

    Let me take you through java code How we can easily implement and use this pattern in java.

    A class A has a method m which takes two parameters String ,String But client who wants to invoke that method does not have a pair of strings but has a Map . So how will they talk . We will insert one more class between then which will act as adaptor . It will take input from client ,will covert the Map parameters in String,String pair combinations and will call the class A method m . Thus client and service class A are not talking with each other directly and Adaptor is making their communication possible.

    Here is example with java code . I have written are steps in system.out.println statements in methods

    Adaptor Design Pattern

    http://efectivejava.blogspot.in/2013/09/java-adaptor-design-pattern.html?utm_source=BP_recent

Leave a Reply


six − = 4



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close