Façade Design Pattern

Façade Design Pattern


  • An interface that provides an easy access to the complex subsystem is called as façade. Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems.
  • One way to achieve this goal is to introduce a “facade” object that provides a single, simplified interface to the many, potentially complex, individual interfaces within the subsystem.
  • Provides a unified interface to a set of interfaces in a subsystem. Façade defines a higher level interface that makes the subsystem easier to use.
  • The subsystem classes still remain available to direct use by the clients.
  • Façade might pass a client request directly to the subsystem or add some extra functionality.

Adapter alters an interface that matches one the client expects and Façade provides a simplified interface for the client.




class MisDepartment

{

  public:

    void submitNetworkRequest()

    {

        _state = 0;

    }

    bool checkOnStatus()

    {

        _state++;

        if (_state == Complete)

          return 1;

        return 0;

    }

  private:

    enum States

    {

        Received, DenyAllKnowledge, ReferClientToFacilities,

          FacilitiesHasNotSentPaperwork, ElectricianIsNotDone,

          ElectricianDidItWrong, DispatchTechnician, SignedOff, DoesNotWork,

          FixElectriciansWiring, Complete

    };

    int _state;

};


class ElectricianUnion

{

  public:

    void submitNetworkRequest()

    {

        _state = 0;

    }

    bool checkOnStatus()

    {

        _state++;

        if (_state == Complete)

          return 1;

        return 0;

    }

  private:

    enum States

    {

        Received, RejectTheForm, SizeTheJob, SmokeAndJokeBreak,

          WaitForAuthorization, DoTheWrongJob, BlameTheEngineer, WaitToPunchOut,

          DoHalfAJob, ComplainToEngineer, GetClarification, CompleteTheJob,

          TurnInThePaperwork, Complete

    };

    int _state;

};


class FacilitiesDepartment

{

  public:

    void submitNetworkRequest()

    {

        _state = 0;

    }

    bool checkOnStatus()

    {

        _state++;

        if (_state == Complete)

          return 1;

        return 0;

    }

  private:

    enum States

    {

        Received, AssignToEngineer, EngineerResearches, RequestIsNotPossible,

          EngineerLeavesCompany, AssignToNewEngineer, NewEngineerResearches,

          ReassignEngineer, EngineerReturns, EngineerResearchesAgain,

          EngineerFillsOutPaperWork, Complete

    };

    int _state;

};


class FacilitiesFacade

{

  public:

    FacilitiesFacade()

    {

        _count = 0;

    }

    void submitNetworkRequest()

    {

        _state = 0;

    }

    bool checkOnStatus()

    {

        _count++;

        /* Job request has just been received */

        if (_state == Received)

        {

            _state++;

            /* Forward the job request to the engineer */

            _engineer.submitNetworkRequest();

            cout << "submitted to Facilities - " << _count << 

              " phone calls so far" << endl;

        }

        else if (_state == SubmitToEngineer)

        {

            /* If engineer is complete, forward to electrician */

            if (_engineer.checkOnStatus())

            {

                _state++;

                _electrician.submitNetworkRequest();

                cout << "submitted to Electrician - " << _count << 

                  " phone calls so far" << endl;

            }

        }

        else if (_state == SubmitToElectrician)

        {

            /* If electrician is complete, forward to technician */

            if (_electrician.checkOnStatus())

            {

                _state++;

                _technician.submitNetworkRequest();

                cout << "submitted to MIS - " << _count << 

                  " phone calls so far" << endl;

            }

        }

        else if (_state == SubmitToTechnician)

        {

            /* If technician is complete, job is done */

            if (_technician.checkOnStatus())

              return 1;

        }

        /* The job is not entirely complete */

        return 0;

    }

    int getNumberOfCalls()


    {

        return _count;

    }

  private:

    enum States

    {

        Received, SubmitToEngineer, SubmitToElectrician, SubmitToTechnician

    };

    int _state;

    int _count;

    FacilitiesDepartment _engineer;

    ElectricianUnion _electrician;

    MisDepartment _technician;

};


int main()

{

  FacilitiesFacade facilities;


  facilities.submitNetworkRequest();

  /* Keep checking until job is complete */

  while (!facilities.checkOnStatus())

    ;

  cout << "job completed after only " << facilities.getNumberOfCalls() << 

    " phone calls" << endl;

}


Facades can be of three types:

1] Transparent – The clients choose to use the classes through the façade for easy implementation of directly.

2] Opaque – The clients can access the classes only through façade.This will isolate the client code from the classes they use.

3] Static – Façade has no state. Clients call the façade without instantiating it.


Coupling between the clients and the subsystem can be further reduced by making façade an abstract class with concrete subclasses for different implementations of the subsystem.


Advantages:

Façade isolates the clients from complex subsystem classes.

Promotes loose coupling between the clients and subsystem classes.

Does not prevent applications from using subsystem classes if they need to.


Disadvantage:

Façade introduce indirections that may affect the performance of an application.


When to use this pattern:

Use the façade pattern when

  • You want to provide simple interface to a complex system.
  • There are many dependencies between clients and the implementation classes of a subsystem.
  • Client makes a lot of network calls to different parts of a service and you want to reduce the number of calls and also provide a simple interface.



Difference between Adapter and Façade:


Adapter

Facade

Adapter converts an incompatible interface into a required one.

Façade provides a simplified interface to a system.

Client cannot access the underlying classes directly.

Clients can still access the underlying modules directly.

Adapter might perform some work before and after the actual class is used.

Façade usually does not perform any processing before delegating the call to the subsystem.

One adapter may work with multiple adaptees at a time.

Façade can work only with one subsystem at a time.


Setup VSCode with Salesforce

Step by Step guide to setup VSCode with Salesforce Install vscode Install Salesforce CLI (developer.salesforce.com/tools/sfdxcli) Perform fo...