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

Adapter Pattern

This pattern is commonly used in applications that have many functions or operations but all of them serve a specific entity.

For example, when you open your computer’s calculator, you will find many modes(Standard, Scientific, ..etc), Standard mode contains basic operations to solve any simple equation or calculation, Scientific mode contains the previous features and other advanced operations.

We can think that the basic operations in the Standard mode have been adapted with the new advanced operations to form the Scientific mode.

This is the idea of the Adapter pattern, we have 2 or more different classes and we want to make them compatible with each other.

The scenario is the following:-
[1] We have a basic standard calculator that contains basic 2 operation, add and subtract
[2] We want to create a calculator that contains the standard operation and other 2 advanced operations which are product and division

The standard calculator is inbuilt contain add and subtract operations

public interface Operations {
        public double calculate(double x, double y, char operation);
    }

The scientific calculator will have division and product operations

public interface AdvancedOperations {
        public double product(double x, double y);
        public double divide(double x, double y);
    }

Now, we will implement the scientific calculator class..

public static class Scientific implements AdvancedOperations {
        @Override
        public double product(double x, double y) {
            return x * y;
        }

        @Override
        public double divide(double x, double y) {
            return x / y;
        }
    }

The next step is to create an adapter, the adapter will use an object of scientific class and implement the standard calculator interface..

public static class CalculatorAdapter implements Operations {
        Scientific scientificMode;

        public CalculatorAdapter(String mode) {
            if (mode.equals("Scientific")) {
                this.scientificMode = new Scientific();
            }
        }

        @Override
        public double calculate(double x, double y, char operation) {
            if (operation == '*') {
                System.out.println("Scientific");
                return this.scientificMode.product(x, y);
            } else if(operation == '/') {
                System.out.println("Scientific");
                return this.scientificMode.divide(x, y);
            }

            if (operation == '+') {
                System.out.println("Standard");
                return x + y; //Standard inbuilt
            } else if (operation == '-') {
                System.out.println("Standard");
                return x - y; // Standard inbuilt
            }

            return 0.0;
        }
    }

Finally, this is our new calculator that supports old and new operations.

public static class Calculator implements Operations {
        CalculatorAdapter calculatorAdapter;

        @Override
        public double calculate(double x, double y, char operation) {
            this.calculatorAdapter = new CalculatorAdapter("Scientific");
            return this.calculatorAdapter.calculate(x, y, operation);
        }
    }

 
You can see the full implementation Here

References:

[1] GoF Book
[2] http://en.wikipedia.org/wiki/Adapter_pattern

Bridge Pattern

The main idea of Bridge pattern is separating the objects’ implementation from its abstraction, this will help when you want to change an object’s implementation code without affecting on its abstraction.

For better understanding, consider the following scenario:-
[1] Vehicle is an abstraction
[2] Cars, buses, motorcycle, …etc are objects that inherit from Vehicle and each one of them has its own implementation
[3] Each vehicle should contain an engine which has different types (Kia, Nissan .. etc), so you can consider engine is an abstraction that all vehicles should have it.

The following code represents an interface that contains some of the vehicles functions

public interface VehicleObject {
        public void move();
        public void accelerate();
        public void stop();
    }

We will create car and bus classes that inherit and implement the above functions

public static class Car implements VehicleObject {
        public void move() {
            System.out.println("Car is moving!");
        }
        
        public void accelerate() {
            System.out.println("Car is accelerating!");
        }
        
        public void stop() {
            System.out.println("Car stopped!");
        }
    }

 

public static class Bus implements VehicleObject {
        public void move() {
            System.out.println("Bus is moving!");
        }
        
        public void accelerate() {
            System.out.println("Bus is accelerating!");
        }
        
        public void stop() {
            System.out.println("Bus stopped!");
        }
    }

This is the implementation phase, each vehicle now has its own implementation.

Each one of these objects should contain an engine, so we will create an Engine interface that holds some of its functions or states.

public interface EngineObject {
        public void running();
        public void stop();
    }
public static class Engine implements engineObject {
        private vehicleObject object;
        
        public void setObject(vehicleObject object) {
            this.object = object;
        }
        
        public void running() {
            this.objectFunctions.move();
        }
        
        public void stopped() {
            this.objectFunctions.stop();
        }
    }

Engine class contains “setObject” to map the engine with its car, engine state depends on the machine (object) state, when the car is moving the engine is running too and vice versa.

We can summarize Bridge pattern implementation in the following points:-
[1] Abstract class
[2] Normal class that represents objects which implement the abstraction
[3] Find an abstraction in the object itself
[4] If it is found, create interface that represent the new abstraction
[5] Implement the new interface

You can see the full implementation Here
 
References:
http://en.wikipedia.org/wiki/Bridge_pattern