Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Facade Design Pattern in C# Example

Rate me:
Please Sign up or sign in to vote.
4.48/5 (13 votes)
8 Mar 2014CPOL2 min read 33.6K   14   5
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.

C#
//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:
C#
//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:
C#
//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:
C#
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)


Written By
Software Developer (Senior)
India India
Linkedin profile: http://www.linkedin.com/profile/view?id=241442098

Comments and Discussions

 
Questiongreat real time scenario Pin
Mohan_J26-Dec-17 23:01
Mohan_J26-Dec-17 23:01 
AnswerRe: great real time scenario Pin
Vipin_Arora27-Dec-17 16:34
Vipin_Arora27-Dec-17 16:34 
GeneralGood Article Pin
Suvendu Shekhar Giri29-Jul-15 2:58
professionalSuvendu Shekhar Giri29-Jul-15 2:58 
GeneralRe: Good Article Pin
Vipin_Arora2-Aug-15 4:44
Vipin_Arora2-Aug-15 4:44 
GeneralMy vote of 5 Pin
D3xtar18-Mar-14 2:39
D3xtar18-Mar-14 2:39 

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

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