Software Design & Documentation Facade Eric Goodnough Software Design & Documentation
About Facade Structural Pattern Provides a unified interface to a set of interfaces in a subsystem Defines a higher-level interface that makes the subsystem easier to use
Structural Diagram Client Classes Facade Subsystem Classes
Participants Facade Subsystem classes Knows which parts of the subsystem are responsible for what Delegates client requests to the appropriate objects in the subsystem Subsystem classes Implement subsystem functionality Handle work assigned by the facade Have no knowledge of the facade, since they keep no reference to it
Applicability Use the facade structural pattern when: You want to provide a simple interface for a complex subsystem There are many dependencies between the client and implementation classes You want to layer your subsystems
Benefits Reduces the number of objects the clients have to deal with Promotes weak coupling between the subsystem and the clients Doesn’t prevent applications from using subsystem classes
Compiler Example Compiler Subsystem: Scanner Parser ProgramNode ProgramNode Builder CodeGenerator Parser Scanner ProgramNodeBuilder CodeGenerator ProgramNode
Example Code (pg. 189-191) Class Scanner {//….}; Class Parser {//….}; Class ProgramNodeBuilder {//….}; Class ProgramNode {//….}; Class CodeGenerator {//….}; Class Compiler { //facade class public: Compiler(); virtual void Compile(istream&, BytecodeStream&); };
Example Code (cont.) Void Compiler::Compile(istream& input, BytecodeStream& output) { Scanner scanner(input); ProgramNodeBuilder builder; Parser parser; parser.Parse(scanner, builder); RISCCodeGenerator generator(output); ProgramNode* parseTree = builder.GetRootNode(); parseTree->Traverse(generator); }
Any Questions?