Facade Pattern
Facade Pattern
Continuing the tour in the structural patterns I'm going to present today the facade pattern.
You can read the my previous posts on structural patterns here: Decorator pattern | Proxy pattern.
What is the Facade Pattern?
The facade pattern is all about encapsulating a subsystem to make it easier to use.
To reduce complexity of systems we usually build them as a union of subsystems.
The facade hides the subsystem implementation and externalize an interface to that subsystem.
The facade interface can have a new set of operations or operations that included in the subsystem.
Another aspect of facade - the facade is an interface to a subsystem implementation and therefore you can remove
the subsystem and put another subsystem instead.
For a UML diagram of the pattern go to dofactory site.
A Facade Story
Recently, I used this pattern to mock a subsystem that is under construction.
In my current project I have two systems that I need to integrate.
The first system is a Blaze Advisor rules engine.
I use the Blaze to develop business rules.
The Blaze is part of the subsystem which also include classes to manipulate responses from the Blaze.
The second system is a mortgage system that sends requests to the Blaze through the facade and get responses from it as system data structures.
I didn't finished the implementations in the Blaze and I needed to test my application.
So what can you do?
I made a xml file with a "response" from the Blaze (I get responses from the Blaze through a web service) and loaded it to the Blaze proxy class.
Then I used the classes that manipulate the response as if I got the response form the Blaze and used the facade class to get the information I needed.
Example in C#
The facade pattern is simple to implement.
Make a namespace with internal classes.
Make a public class that hold those classes and have methods that use the internal classes and you have a facade.
Lets look at a self-explanatory example:
#region The Facade
public class PictureFacade
{
#region Members
private PictureLoader _pictureLoader;
private PictureShower _pictureShower;
#endregion
#region Ctor
public PictureFacade()
{
_pictureLoader = new PictureLoader();
_pictureShower = new PictureShower();
}
#endregion
#region Methods
/// <summary>
/// Constructing an operation of loading the picture first
/// and then showing it
/// </summary>
public void ShowPicture()
{
_pictureLoader.LoadPicture();
_pictureShower.ShowPicture();
}
#endregion
}
#endregion
#region The Subsystem
internal class PictureLoader
{
#region Methods
internal void LoadPicture()
{
Console.WriteLine("Loading a picture");
}
#endregion
}
internal class PictureShower
{
#region Methods
internal void ShowPicture()
{
Console.WriteLine("Showing a picture");
}
#endregion
}
#endregion
Summary
To sum up this post, the facade pattern is helpful to encapsulate a subsystem.
It helps us to look at a very complicated subsystem as a simple interface.
In the next post I'll write about the adapter pattern.