Facade Design Pattern.

Where to use?

   When there are some sub tasks or series of subsystems.

In such cases we need to bother about all the subsystems or we can say we need to remember all the classes, methods and sequence of calling. So to avoid this situation, we can use Facade pattern which provides an interface to client code and hide all the subsystems.

Lets takes an example for facade design pattern. Suppose I need to turn off my Desktop. So the activities involve in this task is : 1. Save my Work and ShutDown Windows. 2. Turn off Monitor 3. Turn off UPS. So there are 3 subsystems and my program looks like: There are 3 different classes with their respective methods.

//Class with Shutdown windows funcion    
public class clsShutDownWindows
    {
        public string ShutDownWindow()
        {
            return "Work Saved and Windows has been Shutdown";
        }
    }


//Class with Monitor Off Function
    public class clsMonitorOff
    {
        public string TurnOffMonitor()
        {
            return "Monitor Turned Off";
        }
    }


//Class with UPS off Function
    public class clsUPSOff
    {
        public string TurnOffUPS()
        {
            return "UPS Turned Off";
        }
    }

Now below is my client code without using facade:

//Here we have created 3 objects of 3 different classes and call their respective method for our task
            clsShutDownWindows ObjPCShutDown = new clsShutDownWindows();
            string sOutput = ObjPCShutDown.ShutDownWindow();

            clsMonitorOff ObjMonitorOff = new clsMonitorOff();
            sOutput = sOutput + "," + ObjMonitorOff.TurnOffMonitor();

            clsUPSOff ObjUPSOff = new clsUPSOff();
            sOutput = sOutput + " and Finally, " + ObjUPSOff.TurnOffUPS();

Now, it would be easy if client calls one method and all three sub tasks execute automatically. So for this we need to implement facade design pattern. For this, we will create a class which will takes care of all 3 subtasks as shown below:
//Class that hides all the sub systems from client
 public class clsTurnOffDesktop
    {
        public string TurnOffDesktop()
        {
            clsShutDownWindows ObjPCShutDown = new clsShutDownWindows();
            string sResult = ObjPCShutDown.ShutDownWindow();

            clsMonitorOff ObjMonitorOff = new clsMonitorOff();
            sResult = sResult + "," + ObjMonitorOff.TurnOffMonitor();

            clsUPSOff ObjUPSOff = new clsUPSOff();
            sResult = sResult + " and Finally, " + ObjUPSOff.TurnOffUPS();

            return sResult;
        }
    }

Now my client code need not worry about 3 different classes and their functions. So finally, with Facade Design pattern, Client code looks like:
            clsTurnOffDesktop ObjTurnOffDesktop = new clsTurnOffDesktop();
            string sOutput= ObjTurnOffDesktop.TurnOffDesktop();
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s