Click here to Skip to main content
12,890,388 members (57,297 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 8 Mar 2014

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#


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


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


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


About the Author

Software Developer (Senior)
India India
Linkedin profile:

You may also be interested in...

Comments and Discussions

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
memberVipin_Arora2-Aug-15 4:44 
GeneralMy vote of 5 Pin
D3xtar18-Mar-14 2:39
memberD3xtar18-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.

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