Facade Design Pattern

Facade is an structural design style that provides a simplified interface for a library, framework, or any other complex set of classes.

Let’s say you need to make your code work with a broad set of objects belonging to a sophisticated library or framework. This typically involves initializing all of these objects, tracking dependencies, executing methods in the correct order, and so on.

As a result, the business logic of the class is tightly coupled with the implementation details of the third-party class, making it difficult to understand and maintain.

Facade is a class that provides a simple interface to a complex subsystem containing many moving parts. Facades can provide limited functionality compared to working directly with subsystems. However, this only includes features that the client really cares about.

Lets look at the examples as below.

PHP Sample Code

<?php

// 3rd party module classes
class Kitchen
{
    public function cook()
    {
        echo "cooking order";
    }
}

class Cashier
{
    public function bill()
    {
        echo "printing bill";
    }
}


//Waiter facade hiding the complexity of other 3rd party classes.
//wrapper class of our all third party packages.
class Waiter
{
    public $kitchen;
    public $cashier;

    public function __construct()
    {
        $this->kitchen = new Kitchen();
        $this->cashier = new Cashier();
    }

    //providing a simple method called order for client
    public function order()
    {
        $this->kitchen->cook();
        $this->cashier->bill();
    }
}


$waiter = new Waiter();
$waiter->order();

C# Sample Code

using System;

namespace HelloWorld
{

    //Waiter facade hiding the complexity of other 3rd party classes.
    //wrapper class of our all third party packages.
    public class Facade
    {
        protected Kitchen _kitchen;

        protected Cashier _cashier;

        public Facade()
        {
            this._kitchen = new Kitchen();
            this._cashier = new Cashier();
        }

        //providing a simple method called order for client
        public string order()
        {
            string result = "";
            result += this._kitchen.cook();
            result += this._cashier.bill();
            return result;
        }
    }

    // 3rd party module classes
    public class Kitchen
    {
        public string cook()
        {
            return "cook order";
        }
    }

    public class Cashier
    {
        public string bill()
        {
            return "print bill";
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            Facade facade = new Facade();
            Console.Write(facade.order());
        }
    }

}

By Yuuma.



アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム