Click here to Skip to main content
Click here to Skip to main content

Facade Design Pattern in C# Example

, 8 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
It is a tip to implement Facade design pattern in c#

Introduction

This tip is useful for basic implementation of Facade Design pattern.

Background

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.

Where to use?

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.


Using the code

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();


Points of Interest

Difference With Adapter pattern: In adapter pattern, Existing code is not accessible. But in this case, Existing code is still visible. You can see more about Adapter pattern from here: Adapter Pattern

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vipin_Arora
Software Developer (Senior)
India India
Linkedin profile: http://www.linkedin.com/profile/view?id=241442098
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberD3xtar18-Mar-14 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 8 Mar 2014
Article Copyright 2014 by Vipin_Arora
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid