Explanation of Facade Design Pattern

Explanation of Facade Design Pattern

Hello everyone, in this article we are going to talk about Facade Design pattern. Firstly we will talk about it and then we will make an example about Facade Design Pattern in C# and Visual Studio.

Let's begin.

What is Facade Design Pattern:

Facade design pattern is a object orientated design pattern within Structural design patterns. We can use Facade design pattern for multi-library applications. Multi-Libraries make the program complex and with facade we will make the accessing these related libraries is getting easy. Our client application make callbacks to the libraries with through the facade class.

If you want you can access the libraries functions. But if related functions declared as public, if they declared as private you can not access them.

Below image you can see the schematic of Facade Design pattern:

Facade Design Pattern Schematic

In the example we are goint to create a console application. In the console application we will create two facade classes. With these classes we will create a computer and a monitor. These classes will use sub parts which require for these main systems. For example we will use RAM, CPU and HDD to build a computer, and we will use Monitor type and Monitor interface to create a monitor. At main program we will just call buildComputer and Build Monitor functions.

Now lets write some code for this example.

First ı will share the sub components of the program. These components RAM, HDD, CPU, and monitor subparts.

These subparts are for Computer building.

CreateCPU.cs


namespace FacadePattern_Example
{
    class CreateCPU
    {
        private string cpuType = "";
        
        public CreateCPU(string _cpu)
        {
            cpuType = _cpu;
        }

        public string getCPU()
        {
            return cpuType;
        }
    }
}

CreateRAM.cs


namespace FacadePattern_Example
{
    class CreateRAM
    {
        private string ramType = "";

        public CreateRAM(string _ram)
        {
            ramType = _ram;
        }

        public string getRAM()
        {
            return ramType;
        }
    }
}

CreateHDD.cs


namespace FacadePattern_Example
{
    class CreateHDD
    {
        private string hddType = "";

        public CreateHDD(string _hdd)
        {
            hddType = _hdd;
        }

        public string getHDD()
        {
            return hddType;
        }
    }
}

These subparts are for Monitor Building.

CreateMonitorInterface.cs


namespace FacadePattern_Example
{
    class CreateMonitorInterface
    {
        private string monInterface = "";

        public CreateMonitorInterface(string _interface)
        {
            monInterface = _interface;
        }

        public string getMonitorInterface()
        {
            return monInterface;
        }
    }
}

CreateMonitorType.cs


namespace FacadePattern_Example
{
    class CreateMonitorType
    {
        private string monType = "";

        public CreateMonitorType(string _type)
        {
            monType = _type;
        }

        public string getMonitorType()
        {
            return monType;
        }
    }
}

Until here our all subparts are ready. And now we are going to create builder classes. You will see the ComputerBuilder class and MonitorBuilder class. These builder classes will use subparts.

Below code blocks you will see code blocks.

BuildComputer.cs


using System;

namespace FacadePattern_Example
{
    class BuildComputer
    {
        private CreateCPU CPU = new CreateCPU("Intel I7 6700U");
        private CreateRAM RAM = new CreateRAM("8GB 1600MHz");
        private CreateHDD HDD = new CreateHDD("2000GB SATA3");

        public void fnc_BuildComputer()
        {
            string comp = "Created Computer is : " + Environment.NewLine;
            comp += "--CPU is : " + CPU.getCPU() + Environment.NewLine;
            comp += "--RAM is : " + RAM.getRAM() + Environment.NewLine;
            comp += "--CPU is : " + HDD.getHDD() + Environment.NewLine;

            Console.WriteLine(comp);
        }
    }
}

BuildMonitor.cs


using System;

namespace FacadePattern_Example
{
    class BuildMonitor
    {
        CreateMonitorInterface monInt = new CreateMonitorInterface("HDMI");
        CreateMonitorType monType = new CreateMonitorType("LED Monitor");

        public void fnc_BuildMonitor()
        {
            string monitor = "Created monitor is : " + Environment.NewLine;
            monitor += "--Monitor Interface is : " + monInt.getMonitorInterface() + Environment.NewLine;
            monitor += "--Monitor Type is : " + monType.getMonitorType() + Environment.NewLine;

            Console.WriteLine(monitor);
        }
    }
}

Now below code blocks you will see how we use these facade classes from our main program.

Program.cs


using System;

namespace FacadePattern_Example
{
    class Program
    {
        static void Main(string[] args)
        {
            BuildComputer buildComputer = new BuildComputer();
            BuildMonitor buildMonitor = new BuildMonitor();

            buildComputer.fnc_BuildComputer();
            buildMonitor.fnc_BuildMonitor();

            Console.ReadLine();
        }
    }
}

As you can see we just declared the components variables and after we calledback the build functions. And we printed the results.

Below image you can see the output of the program.

Facade Design Pattern Output

You can reach the example app from my github page. Visit here : https://github.com/thecodeprogram/FacadePattern_Example

That is all in this article.

I wish you have healthy days.

Burak hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...