Wednesday, May 6, 2015

Facade Pattern - structural design 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 clients 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.
As an example, supposed we have three horribly written classes. For based on the class and method names (and the lack of documentation), it would be very difficult for a client to interact with these classes.
Class1's doSomethingComplicated() method takes an integer and returns its cube.

Class1.java

package com.cakes;

public class Class1 {

 public int doSomethingComplicated(int x) {
  return x * x * x;
 }

}
Class2's doAnotherThing() method doubles the cube of an integer and returns it.

Class2.java

package com.cakes;

public class Class2 {

 public int doAnotherThing(Class1 class1, int x) {
  return 2 * class1.doSomethingComplicated(x);
 }
}
Class3's doMoreStuff() takes a Class1 object, a Class2 object, and an integer and returns twice the sixth power of the integer.

Class3.java

package com.cakes;

public class Class3 {

 public int doMoreStuff(Class1 class1, Class2 class2, int x) {
  return class1.doSomethingComplicated(x) * class2.doAnotherThing(class1, x);
 }

}
For a client unfamiliar with Class1, Class2, and Class3, it would be very difficult to figure out how to interact with these classes. The classes interact and perform tasks in unclear ways. As a result, we need to be able to simplify interaction with this system of classes so that clients can interact with these classes in a simple, standardized manner.
We do this with the Facade class. The Facade class has three methods: cubeX(), cubeXTimes2(), and xToSixthPowerTimes2(). The names of these methods clearly indicate what they do, and these methods hide the interactions of Class1, Class2, and Class3 from client code.

Facade.java

package com.cakes;

public class Facade {

 public int cubeX(int x) {
  Class1 class1 = new Class1();
  return class1.doSomethingComplicated(x);
 }

 public int cubeXTimes2(int x) {
  Class1 class1 = new Class1();
  Class2 class2 = new Class2();
  return class2.doAnotherThing(class1, x);
 }

 public int xToSixthPowerTimes2(int x) {
  Class1 class1 = new Class1();
  Class2 class2 = new Class2();
  Class3 class3 = new Class3();
  return class3.doMoreStuff(class1, class2, x);
 }

}
The FacadeDemo class contains our client code. It creates a Facade object and then calls its three methods with a parameter value of 3. It displays the returned results.

FacadeDemo.java

package com.cakes;

public class FacadeDemo {

 public static void main(String[] args) {

  Facade facade = new Facade();

  int x = 3;
  System.out.println("Cube of " + x + ":" + facade.cubeX(3));
  System.out.println("Cube of " + x + " times 2:" + facade.cubeXTimes2(3));
  System.out.println(x + " to sixth power times 2:" + facade.xToSixthPowerTimes2(3));

 }

}
The console output of the execution of FacadeDemo is shown here.

Console Output

Cube of 3:27
Cube of 3 times 2:54
3 to sixth power times 2:1458
This example demonstrates how the facade pattern can be used to simplify interactions with a system of classes by providing a single point of interaction with the subsystem and hiding the complex details of subsystem interactions from client code. This is accomplished with a Facade class.

Source: http://www.avajava.com/tutorials/lessons/facade-pattern.html

No comments :

Post a Comment