Appearance mode:
Provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes it easier for a subsystem to use.
Visual mode structure
The general implementation of the appearance mode:
- # Include <iostream>
- Using namespace STD;
- 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. It needs to understand the methods and attributes of all subsystems and combine them for external use.
- 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 ();
- }
- ~ Facade ()
- {
- Delete one;
- Delete two;
- Delete three;
- Delete four;
- }
- Void methoda ()
- {
- Cout <"/n method group A () ----" <Endl;
- One-> methodone ();
- Two-> methodtwo ();
- Four-> methodfour ();
- }
- Void methodb ()
- {
- Cout <"/n method group B () ----" <Endl;
- Two-> methodtwo ();
- Three-> methodthree ();
- }
- };
- // Because of the existence of facade, the client does not need to know the existence of the subsystem.
- Int main ()
- {
- Facade * facade = new facade ();
- Facade-> methoda ();
- Facade-> methodb ();
- Delete facade;
- Return 0;
- }
Specific implementation of the facade Mode
- // Investment fund code
- # Include <iostream>
- Using namespace STD;
- // Stock 1
- Class stock1
- {
- // Sell stocks
- Public:
- Void Merge ()
- {
- Cout <"stock 1 sold" <Endl;
- }
- // Buy stock
- Void buy ()
- {
- Cout <"stock 1 buy" <Endl;
- }
- };
- // Stock 2
- Class stock2
- {
- // Sell stocks
- Public:
- Void Merge ()
- {
- Cout <"stock 2 sold" <Endl;
- }
- // Buy stock
- Void buy ()
- {
- Cout <"stock 2 buy" <Endl;
- }
- };
- // Stock 3
- Class stock3
- {
- // Sell stocks
- Public:
- Void Merge ()
- {
- Cout <"stock 3 sold" <Endl;
- }
- // Buy stock
- Void buy ()
- {
- Cout <"stock 3 buy" <Endl;
- }
- };
- // National debt 1
- Class nationaldebt1
- {
- // Sell government bonds
- Public:
- Void Merge ()
- {
- Cout <" 1 sold" <Endl;
- }
- // Buy government bonds
- Void buy ()
- {
- Cout <"National Debt 1 buy" <Endl;
- }
- };
- // Real Estate 1
- Class realty1
- {
- // Sell real estate
- Public:
- Void Merge ()
- {
- Cout <"property 1 sold" <Endl;
- }
- // Buy real estate
- Void buy ()
- {
- Cout <"buy 1 Property" <Endl;
- }
- };
- 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 ();
- }
- ~ Fund ()
- {
- Delete gu1;
- Delete gu2;
- Delete gu3;
- Delete nd1;
- Delete rt1;
- }
- Void buyfund ()
- {
- Gu1-> buy ();
- Gu2-> buy ();
- Gu3-> buy ();
- Nd1-> buy ();
- Rt1-> buy ();
- }
- Void sellfund ()
- {
- Gu1-> callback ();
- Gu2-> callback ();
- Gu3-> callback ();
- Nd1-> encrypt ();
- Rt1-> Reset ();
- }
- };
- Int main ()
- {
- Fund * Fund = new fund ();
- Fund-> buyfund ();
- Fund-> sellfund ();
- Delete fund;
- Return 0;
- }
When to use the appearance mode:
1. at the early stage of the design stage, we must consciously separate two different 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.
2. In the development stage, subsystems often become more and more complex due to continuous reconstruction and evolution. By adding a facade, you can provide a simple interface to reduce the dependency between them.
3. when maintaining a legacy large system, it may be very difficult to maintain and expand. You can develop a facade class for the new system, to provide clear and simple interfaces for the design of rough or highly complex legacy code, so that the new system can interact with the facade object, and the facade and legacy code can interact with all the complicated work.