What is Facade Pattern?: This is a structure design pattern which hides the complexity of the system and provides simple interface to the client. This pattern involves a class called as Facade which provides simplified methods required by clients and delegates its call to methods of existing classes.
Why Facade?: There may be many complex classes in the system. Calling its methods may be tedious and repeated process at many places because of many dependencies between them. So instead of using these methods from these classes again and again, we create a simple class called Façade which utilizes methods from these complex classes in its implementation but provides a simple interface to the client.
Problem: Let’s say you have to draw circle, square and rectangle and you have three different class corresponding to these drawing methods.
Solution: We will create a facade class which provides a simple interface to draw all these three shapes by hiding underlying complexities from the class.
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
public void draw() {
System.debug('Rectangle::draw()');
}
}
public class Square implements Shape {
public void draw() {
System.debug('Square::draw()');
}
}
public class Circle implements Shape {
public void draw() {
System.debug('Circle::draw()');
}
}
public class ShapeFacade {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeFacade() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
In Salesforce, you can use facade design pattern where you are using external web service callouts by providing client a simple interface to get response by hiding underlying complexity of making external callouts.
For all Design Patterns, please refer this.