Design Patterns: Facade

Facade Pattern is a class (complex system) that contains multiple instances of objects (subsystem) each of these objects has its own methods that perform certain tasks. Inside the complex system class it can have one or more methods that wraps one or more subsystem object methods.

To demonstrate this I will use a Car as an example, building car consist of several process or subsystems to build the parts such as wheels, seats, engines and the body. The main system or complex system is to assemble the car.

Below I have a class called Body, Engine, Seats and Wheels, and each of these class contain a method to add certain parts.

The Body class contains a method called AddBody() which takes a enumerated type of BodyType, the body type can be either a sedan, hatch or a suv.

public class Body
{
    public Body ()
    {
    }

    public void AddBody(BodyType bodyType)
    {
        Console.WriteLine ("{0} Body Added", Enum.GetName(typeof(BodyType), bodyType));
    }
}

The Engine class contains a method called AddEngine() which takes an integer value to specify the engine’s cyclinder.

public class Engine
{
    public Engine ()
    {
    }

    public void AddEngine(int cyclinders)
    {
        Console.WriteLine("{0} Cylinder Engine Added", cyclinders.ToString());
    }
}

(more…)

Shares