Facade - Design Pattern

Facade - Design Pattern

Software Architecture Simplified

ยท

2 min read

Objective ๐ŸŽฏ

Hide the complexity of the subsystems by providing a simplified interface to the client.

Type โœ…

โŒBehavioral: Describes how objects interact/communicate between themselves.

โŒCreational: Describes how to instantiate an object without large and complex.

โœ”๏ธStructural: Describes how objects/classes are composed to form larger structures.

UML ๐Ÿ“

image.png

Participants ๐Ÿ”—

โ€ข Facade:

  • Provides simplified methods to be executed by subsystem objects
  • Knows which subsystem classes are responsible for a request

โ€ข Subsystem Classes:

  • Execute some specific funcionality
  • Have no knowledge of the facade and keep no reference to it

Sample Code ๐ŸŽฎ

Structural Example ๐Ÿ›๏ธ

image.png

public static class FacadeStructural
    {
        public static void Execute()
        {
            Facade lFacade = new Facade();
            lFacade.MethodA();
            lFacade.MethodB();
        }
    }

    public class Facade
    {
        private SubSystemOne _SubSystemOne;
        private SubSystemTwo _SubSystemTwo;
        private SubSystemThree _SubSystemThree;
        private SubSystemFour _SubSystemFour;

        public Facade()
        {
            _SubSystemOne = new SubSystemOne();
            _SubSystemTwo = new SubSystemTwo();
            _SubSystemThree = new SubSystemThree();
            _SubSystemFour = new SubSystemFour();
        }
        public void MethodA()
        {
            Console.WriteLine("Facade - Executing Method A");
            _SubSystemOne.MethodOne();
            _SubSystemFour.MethodFour();
        }

        public void MethodB()
        {
            Console.WriteLine("Facade - Executing Method B");
            _SubSystemTwo.MethodTwo();
            _SubSystemThree.MethodThree();
        }
    }

    public class SubSystemOne
    {
        public void MethodOne()
        {
            Console.WriteLine("SubSystemOne - Executing Method One");
        }
    }

    public class SubSystemTwo
    {
        public void MethodTwo()
        {
            Console.WriteLine("SubSystemTwo - Executing Method Two");
        }
    }

    public class SubSystemThree
    {
        public void MethodThree()
        {
            Console.WriteLine("SubSystemThree - Executing Method Three");
        }
    }

    public class SubSystemFour
    {
        public void MethodFour()
        {
            Console.WriteLine("SubSystemFour - Executing Method Four");
        }
    }

Output

image.png

Real-world Example ๐Ÿ”ฅ

image.png

public static class FacadePractical
    {
        public static void Execute()
        {
            OrderFacade lOrderFacade = new OrderFacade();
            lOrderFacade.OrderFood();
        }
    }

    public class OrderFacade
    {
        private Waiter _Waiter;
        private Kitchen _Kitchen;

        public OrderFacade()
        {
            _Waiter = new Waiter();
            _Kitchen = new Kitchen();
        }
        public void OrderFood()
        {
            _Waiter.WriteOrder();
            _Waiter.SendToKitchen();
            _Kitchen.PrepareFood();
            _Kitchen.CallWaiter();
            _Waiter.ServerCustomer();
            _Kitchen.WashDishes();
        }
    }

    public class Waiter
    {
        public void WriteOrder()
        {
            Console.WriteLine("Waiter - Writting Order...");
        }

        public void SendToKitchen()
        {
            Console.WriteLine("Waiter - Sending To Kitchen...");
        }

        public void ServerCustomer()
        {
            Console.WriteLine("Waiter - Serving Customer...");
        }
    }

    public class Kitchen
    {
        public void PrepareFood()
        {
            Console.WriteLine("Kitchen - Preparing Food...");
        }

        public void CallWaiter()
        {
            Console.WriteLine("Kitchen - Calling Waiter...");
        }

        public void WashDishes()
        {
            Console.WriteLine("Kitchen - Washing Dishes...");
        }
    }

Output

image.png

Source Code ๐ŸŽฒ

github.com/VictorLins/DesignPatterns

Did you find this article valuable?

Support Victor Lins by becoming a sponsor. Any amount is appreciated!

ย