Facade Pattern

What is Facade Pattern

The Facade pattern is a structural pattern that provides an entry point to a subsystem of classes. It provides a simpler interface to one or more interfaces. The salient points of a facade pattern are :

  • A facade hides the complexity of a subsystem by providing an interface that is easy to use. It provides a higher level API.
  • A facade decouples the subsystem from the client. The client does not need to be aware of the subsystem interfaces and hence the subsystem interfaces can be modified without informing the client.
  • A facade promotes ‘modular’ development by fixing the contract for a subsystem and letting the subsystem be developed independently.

Example of Facade Pattern

To explain facade pattern we design a very simplistic Bank account creation system. A new user submits the account opening information, which for our example, is the name and social security number (ssn) of the customer. The account opening client takes in these two parameters and creates an account. Before creating the account the client needs to ensure that the ssn is valid and there is no existing account for the name, ssn combination. If the client directly deals with the Account opening interfaces it needs to be aware of the the account verification as well as the account creation interfaces. Lets look at the various classes first

The Bank Account class

					public class Account
{
	// fields made public for brevity.
	public String name;
	public String account_number;
	public String ssn;

}
					

This class has the verification methods for SSN and existing account

public class AccountDetailsVerifier
{

	public boolean isSSNValid(String ssn){
		// verify ssn
		return true;
	}
	
	public boolean isAccountPresent(String name, String ssn){
		// verify address;
		return true;
	}
}
					

This class generates the new account

public class AccountGenerator
{

	public Account createAccount(String name, String account_Number, String ssn)
	{
		Account account = new Account();
		account.name = name;
		account.account_number = account_Number;
		account.ssn = ssn;
		return account;
	}

	public String generateAccountNumber(String name, String ssn)
	{
		return ssn + "" + name.length();
	}
}
					

Lets look at the two clients. The first client is when we have no facade. The second tab shows the facade and the client that uses the facade

This class has the verification methods for SSN and existing account

public class AccountClient
{
	private static AccountDetailsVerifier verifier;
	private static AccountGenerator generator;

	public static void main(String[] args)
	{
		String name = args[0];
		String ssn = args[1];

		verifier = new AccountDetailsVerifier();
		if (!verifier.isSSNValid(ssn)) {
			System.out.println("Account Creation Failed");
			return;
		}
		// check if account for this person (name, ssn combination) is present
		if (verifier.isAccountPresent(name, ssn)) {
			System.out.println("Account Creation Failed");
			return;
		}

		generator = new AccountGenerator();
		String account_no = generator.generateAccountNumber(name, ssn);
		Account account = generator.createAccount(name, account_no, ssn);

	}
	
	
}
					
					

This class generates the new account

public class AccountFacade
{

	private AccountDetailsVerifier verifier;
	private AccountGenerator generator;

	public Account createAccount(String name, String ssn)
	{
		verifier = new AccountDetailsVerifier();
		if (!verifier.isSSNValid(ssn))
			return null;
		// check if account for this person (name, ssn combination) is present
		if (verifier.isAccountPresent(name, ssn))
			return null;
		generator = new AccountGenerator();
		String account_no = generator.generateAccountNumber(name, ssn);
		Account account = generator.createAccount(name, account_no, ssn);
		return account;
	}

}
public class AccountClientUsingFacade
{

	static AccountFacade facade;

	public static void main(String[] args)
	{
		facade = new AccountFacade();
		String name = args[0];
		String ssn = args[1];

		Account account = facade.createAccount(name, ssn);
		if (account == null)
			System.out.println("account creation failed");
	}
}

					

class diagram of Facade Pattern

Facade Design Pattern

If you look at the client without the facade, you will notice that the client has to be aware of the AccountVerifier as well as the Account Generator interface. Also the client needs to be aware of the business logic of account creation. The facade can abstract that out so that the client needs to know only about the Facade. Note that the intent of this pattern is to only hide one or more interfaces to make them easier to use and the client can still control the business logic by passing in appropriate parameters

This finishes our discussion on the facade pattern. One example of real life use of facade pattern is in J2EE where a session facade hides access to multiple session beans.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.