FACADE PATTERN

The facade pattern is a structural design pattern. In the facade pattern, a facade classes is used to provide a single interface to set of classes. The facade simplifies a client’s interaction with a complex system by localizing the interactions into a single interface. As a result, the client can interact with a single object rather than being required to interact directly in complicated ways with the objects that make up the subsystem.

According to GoF Facade design pattern is: Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.

The Facade does not encapsulate the subsystem classes or interfaces; it just provides a simplified interface to their functionality. A client can access these classes directly. It still exposes the full functionality of the system for the clients who may need it. It just provides a layer to the complex interfaces of the sub-system which makes it easier to use.

Facade design pattern is one among the other design patterns that promote loose coupling. It emphasizes one more important aspect of design which is abstraction. By hiding the complexity behind it and exposing a simple interface it achieves abstraction.

Facade can wrap multiple classes, but a facade is used to an interface to simplify the use of the complex interface, whereas, an adapter is used to convert the interface to an interface the client expects.

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.

 

Example:

package com.myjavablog.structural.facade;

public class Calculator1 {

//Calculator1 doSomethingComplex() method takes an integer input and returns cube of it
public float doSomethingComplex(int x){

return x * x * x;
}
}

 

package com.myjavablog.structural.facade;

public class Calculator2 {

//Calculator2 doAnotherThing() method takes an integer input and returns double of cube of it
public float doAnotherThing(Calculator1 c1, int x){

return 2 * c1.doSomethingComplex(x);
}
}

 

package com.myjavablog.structural.facade;

public class Calculator3 {

//Calculator3 doMoreStuff() method takes Calculator1 and Calculator2 objects and multiplies it
public float doMoreStuff(Calculator1 c1, Calculator2 c2 , int x){

return c1.doSomethingComplex(x) * c2.doAnotherThing(c1, x) ;
}
}

 

Facade.java – This class interacts with the base system of classes

package com.myjavablog.structural.facade;

/*
This is the Facade class which interacts with this System of classes
Names of the methods clearly indicates what they do
This methods hides interactions of Calculator1,Calculator2,Calculator3 from client
*/

public class Facade {

public float cubeX(int x){

Calculator1 c1 = new Calculator1();
return c1.doSomethingComplex(x);
}

public float cubeXTimes2(int x){
Calculator1 c1 = new Calculator1();
Calculator2 c2 = new Calculator2();

return c2.doAnotherThing(c1,x);
}

public float multiplyBoth(int x){
Calculator1 c1 = new Calculator1();
Calculator2 c2 = new Calculator2();
Calculator3 c3 = new Calculator3();

return c3.doMoreStuff(c1,c2,x);
}

}

FacadePatternTest.java

package com.myjavablog.structural.facade;

import java.util.Scanner;

//Client class
public class FacadePatternTest {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int x = sc.nextInt();

Facade facade = new Facade();
System.out.println("Cube is "+facade.cubeX(x));
System.out.println("Double of cube is "+facade.cubeXTimes2(x));
System.out.println("Multiplication of both is "+facade.multiplyBoth(x));

}
}

 

Output:

Enter the number:
9
Cube is 729.0
Double of cube is 1458.0
Multiplication of both is 1062882.0

 

Important Points:

  • Facade pattern is more like a helper for client applications; it doesn’t hide subsystem
    interfaces from the client. Whether to use Facade or not is completely dependent on client code.
  • Facade pattern can be applied at any point of development, usually when the number of interfaces grows and system gets complex.
  • Subsystem interfaces are not aware of Facade and they shouldn’t have any reference of the Facade interface.
  • Facade pattern should be applied for similar kind of interfaces; its purpose is to provide a single interface rather than multiple interfaces that does the similar kind of jobs.
  • Subsystem may be dependent with one another. In such case, facade can act as a coordinator and decouple the dependencies between the subsystems.
  • We can use factory design pattern with Facade to provide better interface to client systems.

Usage in Java:

In javax.faces.context, ExternalContext internally uses ServletContext, HttpSession, HttpServletRequest, HttpServletResponse, etc. It allows the Faces API to be unaware of the nature of its containing application environment.

Bitnami