Java

How and when to use Facade Design Pattern?

 

Introduction

Facades are all around us in the real world. Light switch is one example – you can turn of or turn on lights, but you do not know what happens behind the walls, when you press that switch, because they are hiding complexity from the end user.

The Facade Design Pattern hides the complexities of a system and provides an interface to a client from where a client can access a system. It is structural design pattern which main goal is to hide complexity of a system from a client.

Before we introduce Facade design pattern, let’s take a look at official definition of the pattern by GOF:

The Façade Pattern provides unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

When and why I should use it?

Since this is one of the easiest design patterns and it is pretty straightforward you can use it often when you want to hide complexity of the system from the client.

Advantages are:

  • Your code becomes more flexible to changes
  • Unit testing is way simpler
  • You avoid tight coupling between subsystem and its clients.

Disadvantages are:

  • adds a layer of abstraction which may affect performance
  • increase your code base

TIP: You should use Facade design pattern when you are working with 3rd party libraries. You minimize your dependencies upon it and you can choose to move a different library in the future without any problems.

Implementation

We are going to create simple Calculator application that can calculate simple math operations like add, subtract, multiply and divide. We’ll keep it simple as much as possible, since our goal is to demonstrate Facade design pattern, not to make production ready calculator.

Step 1

Create an interface Calculator.

public interface Calculator {

public void calculate(int number1, int number2);
}

Step 2

We are going to create concreate classes that implement our interface:

Add.java

public class Add implements Calculator {

@Override

public void calculate(int number1, int number2) {

int result = number1 + number2;

System.out.println("Result: "+result);

}
}

Subtract.java

public class Subtract implements Calculator{

@Override

public void calculate(int number1, int number2) {

int result = number1 - number2;

System.out.println("Result: "+result);

}
}

Multiply.java

public class Multiply implements Calculator{

@Override

public void calculate(int number1, int number2) {

int result = number1 * number2;

System.out.println("Result: "+result);

}
}

Divide.java

public class Divide implements Calculator{

@Override

public void calculate(int number1, int number2) {

int result = number1 / number2;

System.out.println("Result: "+result);

}

}

Step 3

Now we are going to create façade class that will delegate all calls to appropriate concreate class.

public class CalculatorFacade {

public CalculatorFacade() {
}

public void add(int number1, int number2) {

Add add = new Add();

add.calculate(number1, number2);

}

public void subtract(int number1, int number2) {

Subtract subtract = new Subtract();

subtract.calculate(number1, number2);
}

public void multiply(int number1, int number2) {

Multiply multiply = new Multiply();

multiply.calculate(number1, number2);
}

public void divide(int number1, int number2) {

Divide divide = new Divide();

divide.calculate(number1, number2);
}
}

Step 4

In our main class we will call our façade class and do various calculations.

public class FacadePatternDemo {

public static void main(String[] args) {

CalculatorFacade calculatorFacade = new CalculatorFacade();

calculatorFacade.add(10, 1);

calculatorFacade.subtract(10, 1);

calculatorFacade.divide(10, 2);

calculatorFacade.multiply(10, 2);

}

}

Step 5

Since we have finished our application, let’s run it to see the result.

Result: 11

Result: 9

Result: 5

Result: 20

 

If you are interested in topics like this about development, you can follow me on Twitter @Zookey90.

You Might Also Like

No Comments

Leave a Reply

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