.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Post New Resource Bookmark and Share   

Facade design pattern in C# with example

Posted By:Alankar       Posted Date: April 02, 2015    Points: 200    Category:    URL: http://www.dotnetspark.com  

Design patterns are general reusable solutions to common problems that occurred in software designing. There are broadly 3 categories of design patterns, i.e., Creational, Behavioral and Structural. Facade Design pattern falls under the category of Structural design pattern. As the name suggest it provides an face to implement the subsystems and hides the complexities of subsystems.
 

We often comes under the circumstances where 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 real life 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();

 

 

Note:

Difference With Adapter pattern: In adapter pattern, Existing code is not accessible. But in this case, Existing code is still visible 

 



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend