Big talk design pattern C + + implementation-12th Chapter-Appearance mode

Source: Internet
Author: User

First, UML diagram



Second, the concept

appearance mode: provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes this subsystem easier to use.


Third, the description

Q: When is the appearance mode used?

A: divided into three stages :

(1) First of all, in the early stages of design , it is necessary to consciously separate the different two layers.

(2) Second, in the development phase , subsystems tend to become more and more complex because of the evolution of the continuous reconstruction, most of the patterns will also produce a lot of small classes, this is good, but also to external calls to their user program has brought difficulties in the use of Increasing the appearance of facade can provide a simple interface that reduces dependencies between them.

(3) Thirdly, when maintaining a large legacy system, it may be very difficult to maintain and expand the system, but because it contains very important functions, new requirements development must depend on it. It is also very suitable to use the appearance mode facade.


Specifically: Develop an appearance facade class for the new system to provide a clearer and simpler interface for designing rough or highly complex legacy code, allowing the new system to interact with the facade object and facade all complex work with legacy code.


The benefits are obvious, as shown in:


Iv. implementation of C + +

(1) Facade.h

#ifndef facade_h#define facade_h#include <iostream> #include <string>//subsystem Class to realize the functions of the subsystem, Handles tasks assigned by the facade object. Note that there is no information facade in the subclass, that is, there is no reference to the facade object. Here are four subsystems of class Subsystemone{public:void MethodOne () {std::cout<< "subsystem method One" <<std::endl;}}; Class Subsystemtwo{public:void Methodtwo () {std::cout<< "Subsystem Method II" <<std::endl;}}; Class Subsystemthree{public:void Methodthree () {std::cout<< "subsystem method three" <<std::endl;}}; Class Subsystemfour{public:void Methodfour () {std::cout<< "subsystem method four" <<std::endl;}};/ /facade class, the appearance classes, know which subsystem classes are responsible for processing the request, and proxy the client's request to the appropriate subsystem object. Class facade{private:subsystemone* one; subsystemtwo*; Subsystemthree* three; subsystemfour* Four;public:facade () {one=new subsystemone (); two=new subsystemtwo (); three=new SubSystemThree (); four= New Subsystemfour ();} ~facade () {delete One;delete two;delete three;delete four;} void MethodA () {std::cout<< "method Group A ()------" <<std::endl;one->methodone (); Two->methodtwo (); four- >methodfour ();std::cout<<Std::endl;} void MethodB () {std::cout<< "method Group B ()------" <<std::endl;two->methodtwo (); Three->methodthree (); STD ::cout<<std::endl;}}; #endif


(2) Client.cpp

#include "Facade.h" #include <iostream> #include <cstdlib>//clientvoid main () {facade* facade=new facade (); Facade->methoda (); Facade->methodb ();d elete facade;system ("pause");}


(3) Operation result





Big talk design pattern C + + implementation-12th Chapter-Appearance mode

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.