Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.It helps encapsulate the complexity of multiple subsystems into a single unified interface.
In software terms, Facade pattern hides the complexities of the systems and provides a simple interface to the clients.
This pattern involves one wrapper class which contains a set of methods available for the client. This pattern is particularly used when a system is very complex or difficult to understand and when the system has multiple subsystems.
Component of Facade Design Pattern
- Complex System: A library of subsystems.
- Subsystems: These are classes within a complex system and offer detailed operations.
- Façade: This is a wrapper class which wrapper class which contains a set of members which are required by the client.
- Client: This is a class which calls the high-level operations in the Façade.
Example in C#
Here's an example demonstrating the Facade Pattern in C#.Let's consider a multimedia system with several complex subsystems like the AudioPlayer, VideoPlayer, and DisplaySystem.
- Subsystems:
12345678910111213
namespace
FacadePattern.Subsystem
{
/// <summary>
/// Subsystem 1: AudioPlayer
/// </summary>
public
class
AudioPlayer
{
public
void
PlayAudio(
string
audioFile)
{
Console.WriteLine(
"Playing audio: "
+ audioFile);
}
}
}
12345678910111213namespace
FacadePattern.Subsystem
{
/// <summary>
/// Subsystem 2: VideoPlayer
/// </summary>
public
class
VideoPlayer
{
public
void
PlayVideo(
string
videoFile)
{
Console.WriteLine(
"Playing video: "
+ videoFile);
}
}
}
12345678910111213namespace
FacadePattern.Subsystem
{
/// <summary>
/// Subsystem 3: DisplaySystem
/// </summary>
public
class
DisplaySystem
{
public
void
Display(
string
displayContent)
{
Console.WriteLine(
"Displaying: "
+ displayContent);
}
}
}
- Facade Class(MultimediaFacade.cs):
12345678910111213141516171819202122232425262728293031323334353637
using
FacadePattern.Subsystem;
namespace
FacadePattern.Facade
{
/// <summary>
/// Facade: MultimediaFacade
/// </summary>
public
class
MultimediaFacade
{
private
AudioPlayer audioPlayer;
private
VideoPlayer videoPlayer;
private
DisplaySystem displaySystem;
public
MultimediaFacade()
{
audioPlayer =
new
AudioPlayer();
videoPlayer =
new
VideoPlayer();
displaySystem =
new
DisplaySystem();
}
// Facade methods that provide a simpler interface
public
void
PlayMusic(
string
audioFile)
{
audioPlayer.PlayAudio(audioFile);
}
public
void
PlayMovie(
string
videoFile)
{
videoPlayer.PlayVideo(videoFile);
}
public
void
DisplayContent(
string
content)
{
displaySystem.Display(content);
}
}
}
- Client:
12345678910
using
FacadePattern.Facade;
MultimediaFacade multimedia =
new
MultimediaFacade();
// Using the facade to simplify interaction
multimedia.PlayMusic(
"song.mp3"
);
multimedia.PlayMovie(
"movie.mp4"
);
multimedia.DisplayContent(
"Welcome to our multimedia system!"
);
Console.ReadKey();
In this example, the MultimediaFacade acts as a simplified interface to the complex subsystems (AudioPlayer, VideoPlayer, DisplaySystem). The client code interacts only with the facade, which in turn delegates the tasks to the appropriate subsystems.
Output
When to use this pattern
Use this pattern to simplify the problem when there are multiple complex subsystems and interacting with them individually is really difficult/cumbersome.
Happy coding!! 😊
No comments:
Post a Comment