Introduction to the Facade Pattern

No.of Views1316
Bookmarked0 times
Downloads 
Votes0
By  Chinna Srihari   On  22 Oct 2010 09:10:05
Tag : Design Patterns , General
Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Definition

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Note:

1.    Wrap a complicated subsystem with a simpler interface.
2.    The biggest assumption in Façade pattern is that objects are often Singletons because only one Facade object is required.
3.    Facade defines a new interface
4.    Both, Adapter and Facade are both wrappers. But they are different kinds of wrappers. The intent of Facade is to produce a simpler interface but new, and the intent of Adapter is to design to an existing interface may be one or more. Facade characteristically wraps multiple objects and Adapter wraps a single object.
5.    Abstract Factory can be used as an option to Facade to hide platform-specific classes.

Best examples are

1.    Front desk for any company.
2.    Customer care Help desk.
3.    Amazon.com 1-Click® system

Design

See the basic and Singleton façade examples

 

UML Diagram

 GOF

Image Loading

 

Image Loading

Code

Basic Example

public class SubSystemOne
    {public void MethodOne()
        {
            Console.WriteLine(" Sub System One Method is called");
        }
    }public class SubSystemTwo
    {public void MethodTwo()
        {
            Console.WriteLine(" Sub System Two Method is called");
        }
    }public class SubSystemThree
    {public void MethodThree()
        {
            Console.WriteLine(" Sub System Three Method is called");
        }
    }// "Facade" public class Facade
    {
        FacadeSample.SubSystemOne _subSystemOne;
            FacadeSample.SubSystemTwo _subSystemTwo;
            FacadeSample.SubSystemThree _subSystemThree;

            Facade()
            {
                _subSystemOne = new FacadeSample.SubSystemOne();
                _subSystemTwo = new FacadeSample.SubSystemTwo();
                _subSystemThree = new FacadeSample.SubSystemThree();
            }public void MethodA()
            {
                Console.WriteLine("\n Facade MethodA() is calling ---- ");
                _subSystemOne.MethodOne();
                _subSystemTwo.MethodTwo();
            }public void MethodB()
            {
                Console.WriteLine("\n Facade MethodB() is calling---- ");
                _subSystemTwo.MethodTwo();
                _subSystemThree.MethodThree();
            }public void MethodC()
            {
                Console.WriteLine("\n Facade MethodC() is calling---- ");
                _subSystemOne.MethodOne();
                _subSystemThree.MethodThree();
            }
    }

 Client Side

 

Facade facade = new Facade();
         facade.MethodA();
         facade.MethodB();

In the clasic example aboue, all the sub systems are exposed to the out side world. So we need to restic the access to the sub systems to the out side world.
Make all sub system access modifier to internal.

Also, as said, Façade object should be one instance in the application. So just by changing to static class we can achive this but what if two threads are trying to create? Again it will allow to maintain single instance per thread.

So make sure it should be perfect singleton.

namespace FacadeSample
    {internal class SubSystemOne
        {internal void MethodOne()
            {
                Console.WriteLine(" Sub System One Method is called");
            }
        }internal class SubSystemTwo
        {internal void MethodTwo()
            {
                Console.WriteLine(" Sub System Two Method is called");
            }
        }internal class SubSystemThree
        {internal void MethodThree()
            {
                Console.WriteLine(" Sub System Three Method is called");
            }
        }
    }// "Facade" public class Facade
        {
            FacadeSample.SubSystemOne _subSystemOne;
            FacadeSample.SubSystemTwo _subSystemTwo;
            FacadeSample.SubSystemThree _subSystemThree;

            Facade()
            {
                _subSystemOne = new FacadeSample.SubSystemOne();
                _subSystemTwotwo = new FacadeSample.SubSystemTwo();
                _subSystemThree = new FacadeSample.SubSystemThree();
            }static Facade()
            { 
            }static readonly Facade facadeInstance = new Facade();public static Facade FacadeInstance
            {get { return facadeInstance; }
            }public void MethodA()
            {
                Console.WriteLine("\n Facade MethodA() is calling ---- ");
                _subSystemOne.MethodOne();
                _subSystemTwo.MethodTwo();
            }public void MethodB()
            {
                Console.WriteLine("\n Facade MethodB() is calling---- ");
                _subSystemTwo.MethodTwo();
                _subSystemThree.MethodThree();
            }public void MethodC()
            {
                Console.WriteLine("\n Facade MethodC() is calling---- ");
                _subSystemOne.MethodOne();
                _subSystemThreethree.MethodThree();
            }        }

 Client Side

Facade facade = Facade.FacadeInstance;

            facade.MethodA();
         facade.MethodB();

 Hopes help. thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
Chinna Srihari
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-India
Joined date-16 Jun 2010
Home Page -Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
    Published Date : 23/Oct/2010
    This is a quite simple design pattern, let me tell you one thing there are many design patterns that they are implicitly singletons. I rather recommend to understanding this one first before going through other patterns.
    Published Date : 21/Oct/2010
    As a developer we might have noticed that creation of some class is very difficult and number of time that we need to create. Such object creation every time is very expensive in terms of system resources. If we can cache that kind of object show how will surely boost the application performance and resource usage can be saved. This where object pool design pattern will help development community to cache the objects. Some time it is also called as Object cache or Resource cache design pattern.
    Published Date : 21/Oct/2010
    The Command pattern creates distance between the client that requests an operation and the object that can perform it.
    Published Date : 20/Sep/2010
    A fully initialized instance to be copied or cloned. The Prototype pattern specifies the kind of objects to create using a prototypical instance.
    Published Date : 22/Oct/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top