DCSIMG
Facade Pattern - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

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.

Comments

Lior Israel said:

where is the Interfaces???

# March 16, 2008 9:57 PM

Gil Fink said:

Thanks for your comment Lior.

The Facade design pattern is implemented as a class and not as an interface.

In order to be more clear I changed the interface words to class.

Hope that it helps to make the post more understandable.

# March 17, 2008 7:51 AM

Maor David-Pur said:

The Facade wrap a complicated subsystem with a simpler interface. It's not an interface as Mr.Fink wrote.

# March 18, 2008 12:25 PM

Gil Fink Blog said:

Continuing the tour in the structural patterns I&#39;m going to introduce the Adapter pattern. You can

# March 26, 2008 9:07 PM

John said:

Great site and useful content! Could you leave some opinion about my sites?

<a href="ownsite.com/.../">My pages</a>

[url=http://ownsite.com/b/]My pages[/url]

http://ownsite.com/p/ My pages

# April 5, 2008 10:39 PM

Gil Fink Blog said:

Today it&#39;s the turn of composite pattern to be revealed. You can read my previous posts on structural

# April 11, 2008 2:06 PM

Gil Fink Blog said:

In my first post about design patterns I wrote that there are three kinds of design patterns. I covered

# April 22, 2008 3:59 PM

Gil Fink Blog said:

The first pattern in the creational pattern series is the singleton pattern. You can read my previous

# April 23, 2008 9:14 AM

Gil Fink's Blog said:

This post describe the builder pattern and how to implement it in C#.

# May 17, 2008 9:38 AM

Gil Fink's Blog said:

In this post I explain the prototype design pattern and how it is implemented in C#.

# May 17, 2008 8:13 PM

Gil Fink's Blog said:

In the post I introduce the flyweight design pattern and show a C# example.

# May 18, 2008 8:39 PM

Gil Fink's Blog said:

In the post I describe the bridge design pattern and show an example in C#.

# May 18, 2008 8:48 PM

Gil Fink's Blog said:

In this post I explain the abstract factory design pattern and show an example in C#.

# May 19, 2008 8:46 PM

Gil Fink's Blog said:

In this post I give an introduction to the third design patterns type - the behavioral patterns.

# May 21, 2008 7:15 PM

Gil Fink's Blog said:

In this post I describe the factory method design pattern and how to implement it in C#.

# June 13, 2008 6:24 PM

Gil Fink's Blog said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 12:57 PM

Gil Fink on .Net said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 7:06 PM