Facade Design Pattern in C#

Why Design Pattern:

  • Design patterns provide general solutions to complex problems
  • There are three category of design pattern: Creational, Structural and Behavioral.
  • Facade design pattern comes under structural design pattern.
  • It is used when we want to handle a complex system which has multiple independent subsystems.
  • It provides an abstraction to implement functionality of the complex subsystem.

Why Facade:

Let suppose you want to design a system which will control your all devices in your home and will do following operations when you are going away from home:

Switched off the lights.

Switched off the TV.

Switched off the Music System.

And this system should do same operation in reverse. It means system should switch on all these devices when you are back to home.


Different Parts of Façade design pattern:

• Subsystem: Subsystem is a class which has some functionality that will be accessed by client.

• Facade: This is class which will be the interface for client to access complex subsystem.

• Client: Client class will access the subsystems using Facade class.

C# Code Example:

using System;

namespace FacadeDesignPattern

{

//LightSystem class – SubSystem1

public class LightsSystem

{

public void SwitchOnTheLightsSysstem()

{

Console.WriteLine(“The Lights is Switched ON”);

}

public void SwitchOffTheLights()

{

Console.WriteLine(“The Lights is Switched OFF”);

}

}

//MusicSystem class- Subsystem2

public class MusicSystem

{

public void SwitchOnTheMusicSystem()

{

Console.WriteLine(“The Music System is Switched ON”);

}

public void SwitchOffMusicSystem()

{

Console.WriteLine(“The Music System is Switched OFF”);

}

}

//TVSystem class – Subsystem3

public class TVSystem

{

public void SwitchOnTVSystem()

{

Console.WriteLine(“The TV is Switched ON”);

}

public void SwitchOffTVSystem()

{

Console.WriteLine(“The TV is Switched OFF”);

}

}

public class Facade

{

LightsSystem lightSystem;

MusicSystem musicSystem;

TVSystem tvSystem;

public Facade()

{

lightSystem = new LightsSystem();

musicSystem = new MusicSystem();

tvSystem = new TVSystem();

}

// facade class using subsystems-operation1

public void AwayFromHome()

{

lightSystem.SwitchOffTheLights();

musicSystem.SwitchOffMusicSystem();

tvSystem.SwitchOffTVSystem();

}

// facade class using subsystems – operation2

public void BackToHome()

{

lightSystem.SwitchOnTheLightsSysstem();

musicSystem.SwitchOnTheMusicSystem();

tvSystem.SwitchOnTVSystem();

}

}

//This is client class

class Program

{

static void Main(string[] args)

{

Facade facade = new Facade();

facade.AwayFromHome();

facade.BackToHome();

Console.ReadLine();

}

}

}

Here LightsSystem, MusicSystem and TVSystem are subsystem classes. Facade class is creating objects of these classes and calling these subsystem classes in AwayFromHome method and BackToHome method.

Client can call only AwayFromHome and BackToHome methods to use these subsystems. Hence client does not have access to these subsystems. So Façade class is providing interface to use these subsystem.

Thanks for reading.


1 Comment

Leave a Reply

Your email address will not be published.


*