What is Facade and how to implement in C#?

CsharpServer Side ProgrammingProgramming

The Facade pattern is a simple structure laid over a more complex structure.

The Participants

The Subsystems are any classes or objects which implement functionality but can be "wrapped" or "covered" by the Facade to simplify an interface.

The Facade is the layer of abstraction above the Subsystems, and knows which Subsystem to delegate appropriate work to.

The Facade pattern is so general that it applies to almost every major app especially those where I couldn't refactor or modify pieces of said apps for various reasons.

Example

public class HomeFacade {
   LightsFacade light;
   MusicSystemFacade musicSystem;
   AcFacade ac;
   public HomeFacade() {
      light = new LightsFacade();
      musicSystem = new MusicSystemFacade();
      ac = new AcFacade();
   }
   public void LeaveHomeForOffice() {
      light.SwitchOffLights();
      musicSystem.SwitchOffMusicSystem();
      ac.SwitchOffAc();
   }
   public void ArriveHomeFromOffice() {
      light.SwitchOnLights();
      musicSystem.SwitchOnMusicSystem();
      ac.SwitchOnAc();
   }
}
public class LightsFacade {
   public void SwitchOnLights() {
      Console.WriteLine("Switched on Lights");
   }
   public void SwitchOffLights() {
      Console.WriteLine("Switched off Lights");
   }
}
public class MusicSystemFacade {
   public void SwitchOnMusicSystem() {
      Console.WriteLine("Switched on MusicSystem");
   }
   public void SwitchOffMusicSystem() {
      Console.WriteLine("Switched off MusicSystem");
   }
}
public class AcFacade {
   public void SwitchOnAc() {
      Console.WriteLine("Switched on Ac");
   }
   public void SwitchOffAc() {
      Console.WriteLine("Switched off Ac");
   }
}
raja
Published on 25-Nov-2020 16:44:49
Advertisements