Structural Pattern: Façade When certain elements of the client community need access to the...

Preview:

Citation preview

Structural Pattern: FaçadeWhen certain elements of the client community need access to the functionality provided by a complex subsystem, it is undesirable to expose the client to the intricacies of interacting with this complicated subsystem.

Ch

ap

ter 4

– Pag

e

1

The Façade Pattern provides a unified interface to the subsystem, simplifying the client’s ability to use the subsystem.By encapsulating the complex subsystem within a single interface object, this pattern reduces the learning curve associated with using the subsystem and decouples the subsystem from its clients.

The Façade PatternC

hap

ter 4

– Pag

e

2

Unlike the Adapter Pattern, which makes existing interfaces work together, the Façade Pattern creates an entirely new interface.

The Façade make the subsystem easier to understand, since the Façade has convenient methods for common tasks.By reducing the dependencies between the client and the inner workings of the subsystem, this Pattern allows more flexibility in the development of the subsystem.

ClassA ClassB ClassC

Facade

Class1

Class5

Class2 Class3 Class4

Class6Alternatively, the Façade can wrap a poorly designed collection of APIs with one that is well designed.

Non-Software Façade ExampleThe BridgeControlInterface serves as a Façade between the starship command client and the internal starship systems.

Ch

ap

ter 4

– Pag

e

3

StarshipCommand

WarpDrive

WeaponSystem PropulsionSystem

Holodeck

EmergencyMedicalHologram

BridgeViewscreenImpulseDrive

ImagingSystem

PhaserBank PhotonTorpedo

WarpCore DilithiumChamber

BridgeControlInterface

TransportationSystem

Transporter

Replicator

Turbolift

C++ Façade Pattern Example

Ch

ap

ter 4

– Pag

e

4

#include <iostream>using namespace std;

class ItDepartment{ public: void submitNetworkRequest() { state = 0; } bool checkOnStatus() { state++; if (state == Complete) return true; return false; } private: enum States { Received, DenyAllKnowledge, ReferClientToFacilities, FacilitiesHasNotSentPaperwork, ElectricianIsNotDone, ElectricianDidItWrong, DispatchTechnician, SignedOff, DoesNotWork, FixElectriciansWiring, Complete }; int state; };

Ch

ap

ter 4

– Pag

e

5class ElectricianUnion { public: void submitNetworkRequest() { state = 0; } bool checkOnStatus() { state++; if (state == Complete) return true; return false; } 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 true; return false; } private: enum States { Received, AssignToEngineer, EngineerResearches, RequestIsNotPossible, EngineerLeavesCompany, AssignToNewEngineer, NewEngineerResearches, ReassignEngineer, EngineerReturns, EngineerResearchesAgain, EngineerFillsOutPaperWork, Complete }; int state; };

Ch

ap

ter 4

– Pag

e

6class FacilitiesFacade { public: FacilitiesFacade() { count = 0; } void submitNetworkRequest() { state = 0; } bool checkOnStatus() { count++; /* Job request has just been received */ if (state == Received) { /* Forward the job request to the engineer */ state++; 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 IT - " << count << " phone calls so far" << endl; } }

Ch

ap

ter 4

– Pag

e

7 else if (state == SubmitToTechnician) { /* If technician is complete, job is done */ if (technician.checkOnStatus()) return true; } /* The job is not entirely complete */ return false; } int getNumberOfCalls() { return count; } private: enum States { Received, SubmitToEngineer, SubmitToElectrician, SubmitToTechnician }; int state; int count; FacilitiesDepartment engineer; ElectricianUnion electrician; ItDepartment technician; };

int main(){ FacilitiesFacade facilities; facilities.submitNetworkRequest(); /* Keep checking until job is complete */ while (!facilities.checkOnStatus()); cout << endl << "Job completed after only " << facilities.getNumberOfCalls() << " phone calls" << endl;}

Façade Pattern Advantages

Ch

ap

ter 4

– Pag

e

8

• The Façade Pattern shields users from complex system components while providing a simpler programming interface for general users.

• If detailed access for sophisticated users is needed, therefore, additional access points must be provided to allow for access to underlying classes and methods.

• If the Façade is the only access point to the subsystem, however, it might limit the features and flexibility required by more advanced users.

• The Façade accommodates changes to the underlying subsystem without requiring changes in the client code, and reduces compilation dependencies.

Recommended