<p><strong>Facade Design Pattern</strong> is nothing but it simply interface of interfaces to simplify interactions between the client code and subsystem classes. This design comes under the GOF <strong><a href="https://dineshonjava.com/structural-design-patterns/">Structural Design Pattern</a></strong>. <strong>Facade</strong> 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.</p>
<h2>Facade Design Pattern</h2>
<div style="text-align: left;">
<blockquote><p><strong>According to the Gang of Four:</strong></p>
<div style="text-align: left;">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.</div>
</blockquote>
</div>
<p>This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Patterns</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<h2>UML Class Diagram for Facade Design Pattern</h2>
<p>There are following classes and objects participating in this pattern are:</p>
<p><img class="aligncenter size-full wp-image-3704" src="https://dineshonjava.com/wp-content/uploads/2017/12/facade.gif" alt="Facade Design Pattern" width="380" height="272" /><br />
<strong>Facade (BankingServiceFacade)</strong></p>
<ul>
<li>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.</li>
</ul>
<p><strong>Subsystem classes (AccountService, TransferService, PaymentService)</strong></p>
<ul>
<li>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&#8217;t have implementation details of Facade. These are totally independent from Facade object.</li>
</ul>
<div style="text-align: left;">
<blockquote><p><strong>also read:</strong></p>
<ul style="text-align: left;">
<li><a title="https://dineshonjava.com/adapter-design-pattern/" href="https://dineshonjava.com/adapter-design-pattern/">Adapter Design Pattern</a></li>
<li><a title="https://dineshonjava.com/bridge-design-patterns/" href="https://dineshonjava.com/bridge-design-patterns/">Bridge Design Pattern</a></li>
<li><a title="https://dineshonjava.com/decorator-design-pattern/" href="https://dineshonjava.com/decorator-design-pattern/">Decorator Design Pattern</a></li>
<li><a title="https://dineshonjava.com/proxy-pattern-design-patterns-java/" href="https://dineshonjava.com/proxy-pattern-design-patterns-java/">Proxy Design Pattern</a></li>
<li><a title="https://dineshonjava.com/builder-design-pattern/" href="https://dineshonjava.com/builder-design-pattern/">Builder Design Pattern</a></li>
<li><a title="https://dineshonjava.com/flyweight-pattern-design-patterns-java/" href="https://dineshonjava.com/flyweight-pattern-design-patterns-java/">Flyweight Design Pattern</a></li>
<li><a title="https://dineshonjava.com/creational-design-patterns/" href="https://dineshonjava.com/creational-design-patterns//">Creational GoF Design Patterns</a></li>
<li><a title="https://dineshonjava.com/behavioral-design-pattern/" href="https://dineshonjava.com/behavioral-design-pattern/">Behavioral GoF Design Patterns</a></li>
</ul>
</blockquote>
</div>
<h2>Pros of the Facade Pattern</h2>
<p>There are following pros of using this facade pattern.</p>
<ul>
<li>Facade pattern reduces complexities for client to interact with subsystems.</li>
<li>This pattern consolidates all the business services as single interface to make more understandable.</li>
<li>It reduces dependencies of client code on the inner workings of a system.</li>
</ul>
<h2>Cons of the Facade Pattern</h2>
<p>There are following cons of using this facade pattern.</p>
<ul>
<li>It leads to a larger API to maintain</li>
<li>It hides the important information in useless information</li>
</ul>
<h2>Applicability</h2>
<p>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.</p>
<h2>Sample Implementation of the Facade Design Pattern</h2>
<p>Suppose you want to develop an bank enterprise application with the large number services some of them as <em><strong>AccountService</strong> </em>for getting the <em><strong>Account</strong> </em>by accountId, <em><strong>PaymentService</strong> </em>for payment gateway service and <em><strong>TransferService</strong> </em>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:</p>
<p>This interface is know as Facade interface, it is based the Facade pattern, it is a simple way to interact with the subsystems.</p>
<h3>Step 1: Create an interface.</h3>
<p><strong>Account.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.structural.facade; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public interface Account { 
	 
	void accountType(); 
} 
 
</pre>
<h3>Step 2: Create concrete classes implementing the same interface.</h3>
<p><strong>SavingAccount.java</strong></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.patterns.structural.facade; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class SavingAccount implements Account { 
 
	@Override 
	public void accountType() { 
		System.out.println("SAVING ACCOUNT"); 
	} 
 
} 
 
</pre>
<h3>Step 3: Create a facade interface.</h3>
<p><strong>BankingServiceFacade.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.facade; 
 
public interface BankingServiceFacade { 
	void moneyTransfer(); 
} 
 
</pre>
<h3>Step 4: Create a facade class.</h3>
<p><strong>BankingServiceFacadeImpl.java</strong></p>
<pre class="highlight">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); 
		} 
	} 
} 
 
</pre>
<h3>Step 5: Create a service classes.</h3>
<p><strong>AccountService.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.facade; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class AccountService { 
 
	public static Account getAccount(String accountId) { 
		return new SavingAccount(); 
	} 
} 
 
</pre>
<p><strong>PaymentService.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.facade; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class PaymentService { 
	public static boolean doPayment(){ 
		return true; 
	} 
} 
 
</pre>
<p><strong>TransferService.java</strong></p>
<pre class="highlight">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"); 
	} 
} 
 
</pre>
<h3>Step 6: Use the facade to use various type of services.</h3>
<p><strong>FacadePatternDemo.java</strong></p>
<pre class="highlight">package com.doj.patterns.structural.facade; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class FacadePatternDemo{ 
	public static void main(String[] args) { 
		BankingServiceFacade serviceFacade = new BankingServiceFacadeImpl(); 
		serviceFacade.moneyTransfer(); 
	} 
 
} 
 
</pre>
<h3>Step 7: Let&#8217;s run this demo class and verify the output.</h3>
<pre class="highlight">Transfering Money 
 
</pre>
<h2>Facade Vs Mediator Design Pattern</h2>
<p><strong><a href="https://dineshonjava.com/mediator-pattern-design-patterns-java/">Mediator design pattern</a></strong> may look very similar to<strong> facade design pattern</strong> 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.</p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/decorator-design-pattern/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/core-j2ee-patterns-best-design-practices/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '3703'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…