C + + design mode Shallow view appearance mode

Source: Internet
Author: User
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)!

  • 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.