Facade Design Pattern in C#

Why Design Pattern:

  • Design patterns provide general solutions to complex problems
  • There is three categories 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 that has multiple independent subsystems.
  • It provides an abstraction to implement the functionality of the complex subsystem.

Why Facade:

Let suppose you want to design a system which will control all devices in your home and will do the 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 the same operation in reverse. It means the system should switch on all these devices when you are back home.

 

Different Parts of Façade design pattern:

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

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

• 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 the AwayFromHome method and BackToHome method.

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

Thanks for reading.

 

Hits: 218

1 thought on “Facade Design Pattern in C#”

Leave a comment

0 Shares
Share via
Copy link
Powered by Social Snap