C + + appearance mode and combined mode

Source: Internet
Author: User

The appearance pattern should be used a lot of a pattern, especially when a system is very complex, the system provides to the customer is a simple external interface, and the inside of the complex structure are encapsulated together. Customers simply use these simple interfaces to be able to use the system without having to focus on complex internal structures. DP Book definition: Provides a consistent interface for a set of interfaces in a subsystem, and the appearance pattern defines a high-level interface that makes this subsystem easier to use. As an example of a compiler, suppose it takes four steps to compile a program: Lexical analysis, parsing, intermediate code generation, and machine code generation. Learn to compile all know, each step is very complex. For the compiler this system, you can use the appearance mode. You can define a high-level interface, such as a class named compiler, with a function called run. The client simply calls this function to compile the program, and the client does not need to know what to do inside the run function. A UML diagram is given below, with the compiler as an instance.

#include <iostream>using namespacestd;classscanner{ Public:    voidScan () {cout<<"Lexical Analysis"<<Endl;}};classparser{ Public:    voidParse () {cout<<"Syntax Analysis"<<Endl;}};classgenmidcode{ Public:    voidGencode () {cout<<"Generate intermediate Code"<<Endl;}};classgenmachinecode{ Public:    voidGencode () {cout<<"Generate machine code"<<Endl;}};//high-level interfaceclasscompiler{ Public:    voidRun () {Scanner Scanner;        Parser Parser;        Genmidcode Genmidcode;        Genmachinecode Genmaccode; Scanner.        Scan (); Parser.        Parse ();        Genmidcode.gencode ();    Genmaccode.gencode (); }};intMain () {Compiler cr; Cr.    Run (); return 0;}
appearance mode

This is the appearance mode, which has several features (excerpted from the DP book), (1) It shields the subsystem components from the customer, thus reducing the number of objects processed by the customer and making the subsystem more convenient to use. (2) It realizes the loose coupling between the subsystem and the customer, and the functional components inside the subsystem are often tightly coupled. (3) If the application requires it, it does not restrict their use of subsystem classes.

This example, combined with the above compiler, is further explained. for (1), the compiler class shields the subsystem components from the client, and the client simply handles the compiler's objects to make it easy to use the subsystem. for (2), the changes of the subsystem will not affect the customer's use, reflecting the sub-system and the customer's loose coupling relationship. for (3), if the customer wishes to use a lexical parser, it is not restricted to define the class object of the lexical analysis.

The appearance mode is useful when building large systems. The next step is to introduce another pattern, called the combined pattern. It feels a bit like the appearance pattern, just when we implemented the appearance pattern, the compiler class contained objects of multiple classes, just like combining these classes together. Combination mode is not the meaning, a bit similar, not actually.

The definition given in DP Book: grouping objects into a tree structure to represent a "partial-whole" hierarchy. The combination makes the user consistent with the use of a single object and a composite object. Note the two characters "tree". This tree-like structure is ubiquitous in real life, such as a group company, which has a parent firm with many subsidiaries. Both the parent company and the subsidiary have their own financial department, Human Resources department, sales department and so on. For the parent company, whether it is a subsidiary, or directly under the Finance Department, the Human Resources department, is its department. The entire company's departmental topology is a tree structure.

The following is a UML diagram of the combined pattern. As you can see, Financedepartment, hrdepartment two classes as leaf nodes, so there is no definition to add a function. The Concretecompany class can be used as an intermediate node, so you can add functions. So how do you add it? This class defines a list of elements to be added.

#include <iostream>#include<list>using namespacestd;classcompany{ Public: Company (stringName) {M_name =name;} Virtual~Company () {}Virtual voidADD (Company *PCom) {}    Virtual voidShow (intdepth) {}protected:    stringm_name;};//Specific CompaniesclassConcretecompany: Publiccompany{ Public: Concretecompany (stringname): Company (name) {}Virtual~Concretecompany () {}voidADD (company *pcom) {m_listcompany.push_back (pCom);}//in the middle of the tree, you can add subtrees    voidShow (intdepth) {         for(inti =0; i < depth; i++) cout<<"-"; cout<<m_name<<Endl; List<company *>::iterator iter=M_listcompany.begin ();  for(; ITER! = M_listcompany.end (); iter++)//Show Lower Nodes(*iter)->show (depth +2); }Private: List<company *>M_listcompany;};//Specific Department, finance DepartmentclassFinancedepartment: Publiccompany{ Public: Financedepartment (stringname): Company (name) {}Virtual~financedepartment () {}Virtual voidShow (intDepth//just show, add the function infinitely, because it's a leaf node.    {         for(inti =0; I < depth; i++) cout<<"-"; cout<<m_name<<Endl; }};//specific Department, Human resources departmentclassHrdepartment: Publiccompany{ Public: Hrdepartment (stringname): Company (name) {}Virtual~hrdepartment () {}Virtual voidShow (intDepth//just show, add the function infinitely, because it's a leaf node.    {         for(inti =0; I < depth; i++) cout<<"-"; cout<<m_name<<Endl; }};intMain () { Company*root =NewConcretecompany ("Head Office"); Company*leaf1=NewFinancedepartment ("Finance Department"); Company*leaf2=NewHrdepartment ("HR Department"); Root-Add (LEAF1); Root-Add (LEAF2); //Branch aCompany *MID1 =NewConcretecompany ("Branch a"); Company*leaf3=NewFinancedepartment ("Finance Department"); Company*leaf4=NewHrdepartment ("HR Department"); Mid1-Add (LEAF3); Mid1-Add (LEAF4); Root-Add (MID1); //Branch BCompany *mid2=NewConcretecompany ("Branch B"); Financedepartment*leaf5=NewFinancedepartment ("Finance Department"); Hrdepartment*leaf6=NewHrdepartment ("HR Department"); Mid2-Add (LEAF5); Mid2-Add (LEAF6); Root-Add (MID2); Root->show (0); DeleteLEAF1;Deleteleaf2; DeleteLEAF3;DeleteLEAF4; DeleteLEAF5;DeleteLeaf6; DeleteMID1;DeleteMid2; DeleteRoot; return 0;}
Combination Mode

C + + appearance mode and combined mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.