Appearance mode (facade): 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.
Two major roles:
Appearance class facade: Know which subsystem classes are responsible for processing the request, and proxy the client's request to the appropriate subsystem object.
Subsystem Collection SUBSYSTEM: implements subsystem functions and handles tasks assigned by Facade objects. Note: The subclass does not have any facade information, that is, there is no reference to the facade object.
This article takes the purchase fund as an example, the customer only needs to buy to sell the fund (facade), in which the stock 1, the stock 2, the stock 3 and so on (SubSystem) operation is handled by the fund company.
Test Case:
[Code]int Main () { Fund fund;//Fund Object (facade) fund.fundbuy ();//stock1 buy Stock2 buy stock3 buy Fund.fundsell (); Stock1 sell STCOK2 Sell STOCK3 sell return 0;}
Appearance Mode implementation:
[code]//subsystem does not know any information of facade//Stock 1th class Stock1{public: void Sell () { std::cout << "Stock1 sell\n"; } void Buy () { std::cout << "Stock1 buy\n"; }};/ /Stock no. 2nd Class Stock2{public: void Sell () { std::cout << "Stcok2 sell\n"; } void Buy () { std::cout << "Stock2 buy\n"; }};/ /Stock No. 3rd Class Stock3{public: void Sell () { std::cout << "Stock3 sell\n"; } void Buy () { std::cout << "Stock3 buy\n"; }};/ /Fund category is Facadeclass fund{public: Stock1 stock1; Stock2 Stock2; Stock3 Stock3; void Fundsell () { Stock1.sell (); Stock2.sell (); Stock3.sell (); } void Fundbuy () { stock1.buy (); Stock2.buy (); Stock3.buy (); }};
When do I use appearance mode?
In the early stages of design, it is necessary to consciously separate the different two layers.
In the development phase, subsystems tend to become more and more complex because of the continuous reconfiguration evolution.
When maintaining a large legacy system, the original system's highly complex code or poorly designed code can be used to interact with the facade object in a relatively simple and clear interface.
The above is the C + + design mode of shallow knowledge of the content of the appearance mode, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!