Author - StudySection Post Views - 422 views
Facade Design

Facade Design Pattern

The purpose of the facade design pattern is to provide a complex class with a more user-friendly interface. This reduces the amount of code that must be maintained and makes maintenance easier. This is a structural design pattern that hides the system’s complexity and gives the client a simple interface. This pattern uses a class named Facade, which provides clients with simpler methods while delegating its calls to existing classes’ methods.

Common uses include:

  • Streamlining the Apex Web Service Proxy class’s execution
  • Making custom Apex classes with complex interfaces easier to execute
  • Providing a unified interface for several classes’ functions to be executed (example, multiple web service callouts)

This pattern successfully encapsulates one or more difficult classes, making the remainder of the application’s execution simpler. The facade class’s goal is to make it easier to execute one or more complex classes by providing a more user-friendly interface, thereby improving maintainability.

Why Facade?

The system may contain a large number of complex classes. Because of the various dependencies between its methods, calling them can be a time-consuming and repetitive procedure in many locations. Instead of repeatedly calling these methods from these classes, we develop a simple class called Facade that implements methods from these complex classes while providing a simple interface to the client.

Implementation of Facade Design Pattern –
To implement a facade class, simply build a new class that abstracts the complex class’s implementation. This facade class normally has a more user-friendly interface and, in some situations, orchestrates the execution of numerous complex classes.

The code sample below shows a facade class that calls numerous web services to arrange the establishment of an account and contact in an external system:

Shape Class –

public interface Shape {
void drawShape();
}

Rectangle Class –

public class Rectangle implements Shape {
public void drawShape() {
System.debug('Rectangle Shape::draw()');
}
}

Square Class –

public class Square implements Shape {
public void drawShape() {
System.debug('Square Shape::draw()');
}
}

Circle Class –

public class Circle implements Shape {
public void drawShape() {
System.debug('Circle Shape::draw()');
}
}

Facade Class –

public class CreateShapeFacade {
private Shape circle;
private Shape rectangle;
private Shape square;
public CreateShapeFacade() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.drawShape();
}
public void drawRectangle(){
rectangle.drawShape();
}
public void drawSquare(){
square.drawShape();
}
}

Call to Facade Class (through Anonymous Window) –

CreateShapeFacade createShape = new CreateShapeFacade();
createShape.drawCircle();
createShape.drawSquare();
createShape.drawRectangle();

Here, as you can see, the facade design pattern enhances Apex code maintainability by using a facade class to simplify the execution of one or more complex classes.

jQuery presents a tree-like structure of all the elements on a webpage simplifying the syntax and further manipulating such elements. The jQuery Certification exam by StudySection will secure your fundamental knowledge and a basic understanding of jQuery as an asset to improve your skills.

Leave a Reply

Your email address will not be published.