CHAPTER 11

image

Facade Patterns

GoF Definition: Provide a unified interface to a set of interfaces in a system. Facade defines a higher-level interface that makes the subsystem easier to use.

Concept

It is one of those patterns that supports loose coupling. Here we emphasize the abstraction and hide the complex details by exposing a simple interface.

Real-Life Example

Suppose you are going to organize a birthday party and you have invited 100 people. Nowadays, you can go to any party organizer and let him/her know the minimum information— (party type, date and time of the party, number of attendees, etc.). The organizer will do the rest for you. You do not even think about how he/she will decorate the party room, whether people will take food from self-help counter or will be served by a caterer, and so on.

Computer World Example

We can think about a case where we use a method from a library. The user doesn’t care how the method is implemented in the library. He/she just calls the method to serve his/her easy purpose. The pattern can be best described by the example that follows.

Illustration

Here our aim is to build/construct robots. And from a user point of view he/she needs to supply only the color and material for his/her robot through the RobotFacade (See our FacadePatternEx.java file.) Our RobotFacade (RobotFacade.java) will    in turn create objects for RobotBody, RobotColor, RobotMetal and will do the rest for the user. We need not worry about the creation of these separate classes and their calling sequence. All of the classes have their corresponding implementation here.

In this example I have followed this structure. Note that the related parts are separated by the packages for better readability.

UML Class Diagram

9781484218013_unFig11-01.jpg

Package Explorer view

9781484218013_unFig11-02.jpg

Implementation

//RobotBody.java
package robotparts;

public class RobotBody
{
        public void CreateBody()
        {
                System.out.println("Body Creation done");
        }
}

//RobotColor.java
package robotparts;

public class RobotColor
{
        private String color;
        public void SetColor(String color)
        {
                this.color = color;
                System.out.println("Color is set to : "+ this.color);
        }
}

//RobotMetal.java
package robotparts;

public class RobotMetal
{
        private String metal;
        public void SetMetal(String metal)
        {
                this.metal=metal;
                System.out.println("Metal is set to : "+this.metal);
        }
}

//RobotFacade.java
package robotfacade;
import robotparts.*;

public class RobotFacade
{
        RobotColor rc;
        RobotMetal rm ;
        RobotBody rb;
        public RobotFacade()
        {
                rc = new RobotColor();
                rm = new RobotMetal();
                rb = new RobotBody();

        }
        public void ConstructRobot(String color,String metal)
        {
                System.out.println(" Creation of the Robot Start");
                rc.SetColor(color);
                rm.SetMetal(metal);
                rb.CreateBody();
                System.out.println(" Robot Creation End");
                System.out.println();
        }
}
//FacadePatternEx.java
package facade.pattern.demo;
import robotfacade.RobotFacade;

class FacadePatternEx
{
        public static void main(String[] args)
        {
                System.out.println("***Facade Pattern Demo***");
                RobotFacade rf1 = new RobotFacade();
                rf1.ConstructRobot("Green", "Iron");
                RobotFacade rf2 = new RobotFacade();
                rf2.ConstructRobot("Blue", "Steel");

        }
}

Output

9781484218013_unFig11-03.jpg

Note

  1. We use Facade pattern to represent a simple interface instead of a complex subsystem.
  2. Here we promote weak coupling among subsystems—so, in this way, we are making them portable.
  3. We already mentioned that we separate subsystems from clients by a simple interface. With this model, we not only make the system easier to use but also reduce the number of objects that the clients need to deal with.
  4. There is truly no major disadvantage associated with this pattern. On the other hand, it has proven its usefulness in libraries like jQuery also.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.135.249