Design Patterns - Facade

design-patterns

// Design Patterns - Structural - Facade:

Facade pattern hides the complexities of the system and provides an interface 
to the client using which the client can access the system. This type of design 
pattern comes under structural pattern as this pattern adds an interface to 
existing system to hide its complexities.

This pattern involves a single class which provides simplified methods required 
by client and delegates calls to methods of existing system classes.

// Step 1: Create an interface.
public interface Shape {
   void draw();
}

// Step 2: Create concrete classes implementing the same interface.
public class Rectangle implements Shape {
   public void draw() {
      System.out.println("Rectangle::draw()");
   }
}

public class Square implements Shape {
   public void draw() {
      System.out.println("Square::draw()");
   }
}

public class Circle implements Shape {
   public void draw() {
      System.out.println("Circle::draw()");
   }
}

// Step 3: Create a facade class.
public class ShapeMaker {
   private Shape circle;
   private Shape rectangle;
   private Shape square;

   public ShapeMaker() {
      circle = new Circle();
      rectangle = new Rectangle();
      square = new Square();
   }

   public void drawCircle(){
      circle.draw();
   }
   public void drawRectangle(){
      rectangle.draw();
   }
   public void drawSquare(){
      square.draw();
   }
}

// Step 4: Use the facade to draw various types of shapes.
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();

What is the purpose of the Facade pattern?

A facade is, in fact, a class wrapping a complex library to provide a simpler and more readable interface to it. The Facade pattern can also be used to provide a unified and well-designed API to a group of complex and poorly designed APIs.

A facade is like an API that simplify a complex subsystem:

facade_design_pattern.jpg

Assume that you have a few operations to be made in sequence, and that the same action is required in multiple places within your application. You have to place the same code again and again in different places. You have done that, but after a few days you find that something needs to be changed in that code. We have to introduce the changes in all of the places that the code exists. It's painful, isn't it?

As a solution, what you should be doing is to create a lead controller, which handles all of the repeating code. From the calling point of view, we will just call the lead controller to perform actions based on the parameters provided. Now if we need to introduce any change in the process, then we will just need to change the lead controller instead of making the change in all places where we used that code.

Facade: A single class that represent an entire subsystem. This reduce coupling when dealing with third party libraries or providers. Event Managers are good facade. The event manager takes care of decoration, food, invitations, room reservation, entertainment, etc. We just tell the facade what we need, and it take care of the rest.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License