Download presentation
Presentation is loading. Please wait.
Published byChristiana Harper Modified over 9 years ago
2
Structural Pattern: Façade When 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. C h a p t e r 4 – P a g e 101 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.
3
The Façade Pattern C h a p t e r 4 – P a g e 102 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. Alternatively, the Façade can wrap a poorly designed collection of APIs with one that is well designed.
4
Non-Software Façade Example The BridgeControlInterface serves as a Façade between the starship command client and the internal starship systems. C h a p t e r 4 – P a g e 103
5
C++ Façade Pattern Example C h a p t e r 4 – P a g e 104 #include 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; };
6
C h a p t e r 4 – P a g e 105 class 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; };
7
C h a p t e r 4 – P a g e 106 class 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; }
8
C h a p t e r 4 – P a g e 107 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; }
9
Façade Pattern Advantages C h a p t e r 4 – P a g e 108 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.