Home > Backend Development > C#.Net Tutorial > A brief introduction to C++ design patterns and appearance patterns

A brief introduction to C++ design patterns and appearance patterns

黄舟
Release: 2017-01-18 14:52:47
Original
1618 people have browsed it

Facade mode (Facade): Provides a consistent interface for a set of interfaces in a subsystem. This mode defines a high-level interface that makes this subsystem easier to use.

Two major roles:

Appearance class Facade: Knows which subsystem classes are responsible for processing requests, and proxies customer requests to the appropriate subsystem objects.

Subsystem collection SubSystem: implements the functions of the subsystem and handles tasks assigned by the Facade object. Note: The subclass does not have any information about the Facade, that is, there is no reference to the Facade object.

This article takes the purchase of funds as an example. Customers only need to buy and sell funds (Facade), and the operations of Stock 1, Stock 2, Stock 3, etc. (SubSystem) are handled by the fund company.

Test case:

[code]int main(){
    Fund fund;//基金对象(Facade)
    fund.fundBuy();//stock1 buy stock2 buy stock3 buy
    fund.fundSell(); //stock1 sell stcok2 sell stock3 sell

    return 0;
}
Copy after login

Appearance mode implementation:

[code]//子系统不知道Facade的任何信息
//股票1号
class Stock1{
public:
    void sell(){
        std::cout << "stock1 sell\n";
    }
    void buy(){
        std::cout << "stock1 buy\n";
    }
};
//股票2号
class Stock2{
public:
    void sell(){
        std::cout << "stcok2 sell\n";
    }
    void buy(){
        std::cout << "stock2 buy\n";
    }
};
//股票3号
class Stock3{
public:
    void sell(){
        std::cout << "stock3 sell\n";
    }
    void buy(){
        std::cout << "stock3 buy\n";
    }
};

//基金类是Facade
class Fund{
public:
    Stock1 stock1;
    Stock2 stock2;
    Stock3 stock3;
    void fundSell(){
        stock1.sell();
        stock2.sell();
        stock3.sell();
    }
    void fundBuy(){
        stock1.buy();
        stock2.buy();
        stock3.buy();
    }
};
Copy after login

When to use appearance mode?

In the early stages of design, you should consciously separate the two different layers.

During the development stage, subsystems often become more and more complex due to continuous reconstruction and evolution.

When maintaining a large legacy system, you can use the highly complex code or poorly designed code of the original system to use a relatively simple and clear interface to allow the new system to interact with the Facade object.

The above is the content of the appearance mode of C++ design pattern. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template