Facade Pattern

One of the most important patterns that you should deal with when you are working on huge systems which have many objects you are interact with.

 

Pattern’s Idea:-

You may find yourself dealing with many objects in your application, it will be good if there is away to deal with all system objects through few ones, this is the main idea or the main motivation to create the Facade pattern, as the meaning of “Facade”, this pattern builds a layer between the programmer and the other system’s objects, the programmer interacts with these objects through only one object.

test

For better understanding this pattern, assume that you want to build a form application, the form may contains labels, text boxes and buttons, each one of these handlers has some properties you could change.

public abstract class Design
{
    public abstract void setPosition(double x, double y);
    public abstract void setWidth(double x);
    public abstract void setHeight(double y);
    public abstract void setColor(int r, int g, int b);
}

We have an abstract class that has the handler’s properties, we will use these methods to create each handler’s class.

public class TextBox : Design
{
    public override void setPosition(double x, double y)
    {
        Console.WriteLine("TextBox has been set at " + x + " " + y);
    }

     public override void setWidth(double x)
     {
         Console.WriteLine("TextBox width is " + x + " now");
     }

     public override void setHeight(double y)
     {
         Console.WriteLine("TextBox Height is " + y + " now");
     }

     public override void setColor(int r, int g, int b)
     {

     }
}

 

public class Button : Design
{
    public override void setPosition(double x, double y)
    {
        Console.WriteLine("Button has been set at " + x + " " + y);
    }

    public override void setWidth(double x)
    {
        Console.WriteLine("Button width is " + x + " now");
    }

    public override void setHeight(double y)
    {
        Console.WriteLine("Button Height is " + y + " now");
    }

    public override void setColor(int r, int g, int b)
    {
        Console.WriteLine("Button Color is set!");
    }
}

 

The Structure:-

UMLFacade

Let’s create a Facade class that considered to be a layer which enables the programmer to get access to each handler through this class.

public class GraphicalUserInterface
{
    private Design textBox;
    private Design button;
    private Design label;

    public GraphicalUserInterface()
    {
        this.textBox = new TextBox();
        this.label = new Label();
        this.button = new Button();
    }

    public void createTextBox()
    {
        this.textBox.setPosition(1.0, 1.0);
        this.textBox.setWidth(1.0);
        this.textBox.setHeight(1.0);
    }

    public void createLabel()
    {
        this.label.setPosition(2.0, 2.0);
        this.label.setWidth(2.0);
        this.label.setHeight(2.0);
    }

    public void createButton()
    {
        this.button.setPosition(3.0, 3.0);
        this.button.setWidth(3.0);
        this.button.setHeight(3.0);
    }
}

Finally, create the facade object which represents the other objects..

static void Main(string[] args)
{
     GraphicalUserInterface buildGUI = new GraphicalUserInterface();

     buildGUI.createButton();
     buildGUI.createLabel();
     buildGUI.createTextBox();
}

 

Advantages:-

[1] Reduce system objects
[2] Simplify huge systems to subsystems

You can see the full implementation HERE

References:
Design Patterns Explained Book

Advertisements

2 thoughts on “Facade Pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s