[Design mode] -- appearance Mode

Source: Internet
Author: User

The appearance mode (facade) provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use.

Appearance mode structure:

Code Template:

// Class of the four subsystems
Class subsystemone
{
Public:
Void methodone ()
{
Cout <"Subsystem Method 1" <Endl;
}
};
Class subsystemtwo
{
Public:
Void methodtwo ()
{
Cout <"Subsystem Method 2" <Endl;
}
};
Class subsystemthree
{
Public:
Void methodthree ()
{
Cout <"Subsystem Method 3" <Endl;
}
};
Class subsystemfour
{
Public:
Void methodfour ()
{
Cout <"Subsystem Method 4" <Endl;
}
};
// Appearance class, which needs to understand the methods or attributes of all subsystems
Class Facade
{
PRIVATE:
Subsystemone * one;
Subsystemtwo * Two;
Subsystemthree * three;
Subsystemfour * four;
Public:
Facade ()
{
One = new subsystemone ();
Two = new subsystemtwo ();
Three = new subsystemthree ();
Four = new subsystemfour ();
}
Void methoda ()
{
Cout <"method group A () ---" <Endl;
One-> methodone ();
Two-> methodtwo ();
Four-> methodfour ();
}
Void methodb ()
{
Cout <"method group B () ---" <Endl;
Two-> methodtwo ();
Three-> methodthree ();
}
};
// Client call. Due to the role of facede, the client may not know the existence of the sub-system class.
Int main ()
{
Facade * facade = new facade ();
Facade-> methoda ();
Facade-> methodb ();
Return 0;
}

The following is an example of stock trading:

Code structure:

Class stock1 {public: void consumer () {cout <"stock 1 sold" <Endl;} void buy () {cout <"stock 1 buy" <Endl ;}; class stock2 {public: void buy () {cout <"stock 2 sell" <Endl ;} void buy () {cout <"stock 2 buy" <Endl ;}}; class stock3 {public: void Merge () {cout <"stock 3 sell" <Endl ;}void buy () {cout <"stock 3 buy" <Endl ;}}; class nationaldebt1 {public: void consumer () {cout <" 1 sell" <Endl ;}void buy () {cout <" 1 buy" <Endl ;}}; class realty1 {public: void buy () {cout <"Real Estate 1 sold" <Endl;} void buy () {cout <"Real Estate 1 buy" <Endl ;};// appearance class fund {PRIVATE: stock1 * gu1; stock2 * gu2; stock3 * gu3; nationaldebt1 * nd1; realty1 * rt1; public: Fund () {gu1 = new stock1 (); gu2 = new stock2 (); gu3 = new stock3 (); nd1 = new nationaldebt1 (); rt1 = new realty1 ();} void buyfund () {gu1-> buy (); gu2-> buy (); gu3-> buy (); nd1-> buy (); rt1-> buy ();} void sellfund () {gu1-> terminal (); gu2-> terminal (); gu3-> round (); nd1-> round (); rt1-> round () ;}}; int main () {Fund * jijin = new fund (); jijin-> buyfund (); jijin-> sellfund (); Return 0 ;}

The appearance mode should be used in three stages. First, in the initial stage of design, we should consciously separate the two layers, such as the classic three-layer architecture, you need to consider establishing an external facade between the data access layer and the business logic layer, the business logic layer, and the presentation layer Layer. In this way, you can provide a simple interface for complicated subsystems, this greatly reduces coupling. Secondly, in the development stage, subsystems are often increasingly complicated due to the continuous reconstruction and evolution. Many small classes will be generated in most modes. This is a good thing, however, it also makes it difficult for external users to call their user programs. Increasing the appearance mode can provide a simple interface to reduce their dependencies. Third, when maintaining a legacy large system, it may be very difficult to maintain and expand, but because it contains very important functions, new demand development must rely on it. The appearance mode is also very suitable. You can develop a facade class for the new system to provide clear and simple interfaces for designing rough or highly complex legacy code, so that the new system can interact with the facade object, facade interacts with legacy code for all complex tasks.

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.