Problem focus: when designing a system, a common practice is to divide it into several small systems and collaborate with each other to form a large system. So what do you need to pay attention to when designing these minor systems? Why does the appearance mode make the subsystem easier to use? (To minimize the communication and dependency between subsystems, that is, to reduce the Coupling Degree)
Intent: provide a consistent interface for a group of interfaces in the subsystem. The Facade mode defines a high-level interface, which makes this subsystem easier to use.
Motivation: dividing a system into several subsystems helps reduce system complexity. A common design goal is to minimize the communication and dependency between subsystems. One way to achieve this goal is to introduce an appearance object, which provides a single and simple interface for the more common facilities in the subsystem. The idea is as follows: Demo: Taking the design of the compiler subsystem as an example, describe the idea of the appearance mode: Sub-system class division: Parser, Parser, ProgramNode, BytecodeStream, ProgramNodeBuilder requirements: A single and simple compilation sub-system interface provides high-level functions for common users, allowing them to directly access specific sub-system classes. DESIGN: Compiler class (key), which has two responsibilities: 1) shielding sub-systems for users and providing a high-level simple interface for use; 2) the sub-system classes that implement compilation functions are not completely hidden, so that a few people who know how to use the underlying functions can call these functions. Applicability: Use the Facade mode in the following situations:
- Provides a simple interface for a complex subsystem.
- There is a large dependency between the implementation part of the client program and the abstract class.
- When you need to build a layered sub-system, use the facade mode to define the entry points for each layer of the sub-system. If subsystems are mutually dependent, they can communicate only through facade, thus simplifying the dependency between them. Structure: vc/Platform + 09DA + 9PavPK7r8 + Platform + yOe5 + platform/LKisrvP3tbGy/Platform + vbW1zb/Platform + platform/zwODKtc/WRmFjYWRltvjL/LXEvt/release K/keys + 1zbO21M/zvfjQ0Mzmu7u8tL/keys + keys/ry7y/keys/MPHtrzT0L3Tv9qyosfSy/zDx7a8t + keys AgICDX08 + release/release + release/avfbTw9PattTX08 + 1zbO9 + release/atcTSu7K/t9ajrLWry/release + sLryr7A/aO6CgrS1MnPw + bL + release /Zt + labels/J08PT2ta4tqi7 + labels + sn60ru49rHqyra3 + labels "brush: java;"> class labels {public: libraries (istream &); virtual ~ Token (); virtual Token & Scan (); private: istream & _ inputStream ;};Parser: the Parser class consists of the identifier generated by the Parser to construct a syntax analysis tree.
class Parser{public: Parser(); virtual ~Parser(); virtual void Parse(Scanner&, ProgramNodeBuilder&);};ProgramNodeBuilder: Creates a syntax tree and follows the interactive operations in the Builder mode.
class ProgramNodeBuilder{public: ProgramNodeBuilder(); virtual ProgramNode* NewVariable ( const char* variableName ) const; virtual ProgramNode* NewAssignment ( ProgramNode* variable, ProgramNode* expression ) const; virtual ProgramNode* NewRetrurnStatement ( ProgramNode* value ) const; virtual ProgramNode* NewCondition ( ProgramNode* condition, ProgramNode* truePart, ProgramNode* falsePart ) const; ProgramNode* _node;private: ProgramNode* _node;};ProgramNode: the syntax tree consists of instances of the ProgramNode subclass (StatementNode and ExpressionNode). It defines an interface for operating program nodes and Its subnodes. The ProgramNode hierarchy is an application instance of the Composite mode.
class ProgramNode {public: virtual void GetSourcePositon(int& line, int& index); virtual void Add(ProgramNode*); virtual void Remove(ProgramNode*); virtual void Traverse(CodeGenerator&);protected: ProgramNode();};Traverse: Takes a CodeGenerator object as the parameter. The ProgramNode subclass uses this object to generate machine code. The machine code format is the ByteCode object in BytecodeStream.
CodeGenerator: A visitor (visitor mode). It has two sub-classes: StackMachineCodeGenerator and RISCCodeGenerator, which generate machine code for different hardware architectures.
class CodeGenerator {public: virtual void Visit(StatementNode*); virtual void Visit(ExpressionNode*);protected: CodeGenerator(BytecodeStream&);protected: BytecodeStream& _output;};ExpressionNode: each subclass of ProgramNode calls Traverse for its ProgramNode sub-object when implementing Traverse. Each subclass performs the same action on its subnodes at a time. This recursion goes on.
void ExpressionNode::Traverse (CodeGenerator& cg) { cg.Visit(this); ListIterator
i(_children); for (i.First(); !i.IsDone(); i.Next()) { i.CurrentItem()->Traverse(cg); }}
These classes constitute the compilation subsystem. REFERENCE The Compiler class to integrate all components. Compiler provides a simple interface for compiling source code and generating executable code for a specific machine.
class Compiler{public: Compiler(); virtual void Compile(istream&, BytecodeStream);};void Compiler::Compile(istream& input, BytecodeStream& output){ Scanner scanner(input); ProgramNodeBuilder builder; Parser parser; parser.Parse(scanner, builder); RISCCodeGenerator generator(builder); ProgramNode* parseTree = builder.GetRootNode(); parserTree->Traverse(generator);}
Related mode: the abstract factory mode can be used with the Facade mode to provide an interface, which can be used to create subsystem objects in a seed system independent way. Abstract Factory mode can also replace Facade mode to hide platform-related classes. Mediator mode, which generally does not belong to any single object. The Facade mode does not define new functions, and the subsystem does not know that the existence of the facade usually requires only one Facade object. Therefore, the Facade Object usually belongs to the Singleton mode.
Reference: Design Pattern: Basis for reusable Object-Oriented Software