Facade façade mode definition: Some people also call it appearance mode
Provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes the subsystem easier to use.
Mode Description:
Subsystem (SUBSYSTEM): Responsible for dealing with complex logical processing. Do not deal directly with customers.
Façade (facade): Responsible for interacting with subsystems, providing easy-to-use functions or interfaces to customers.
Customer (client): Interact with the façade and subsystem without needing to be concerned about the details of the subsystem.
Main functions:
- Simplifies complex interfaces,
- Decoupling and masking the client's direct access to the subsystem
C + + implementation code (online Daniel offers)
1#include <iostream>2 using namespacestd;3 4 //Camera Class5 classCamera6 {7 Public:8 Camera ()9 {Ten One } A -~Camera () - { the - } - //how the camera opens - voidTrunon () + { -cout <<"Camera Open!"<<Endl; + } A //How to turn off the camera at voidTrunoff () - { -cout <<"Camera off!"<<Endl; - } - //method of camera angle adjustment - voidRotate (intdegrees) in { -cout <<"Rotate the camera by"<< degrees <<"degrees!"<<Endl; to } + }; - the //Light Class * class Light $ {Panax Notoginseng Public: - Light () the { + A } the +~Light () - { $ $ } - //How to open the light - voidTrunon () the { -cout <<"Lights Open!"<<Endl;Wuyi } the //method of light off - voidTrunoff () Wu { -cout <<"Lights off!"<<Endl; About } $ }; - - //Screen Class - class Screen A { + Public: the Screen () - { $ the } the the~Screen () the { - in } the //Display Open Method the voidTrunon () About { thecout <<"The monitor is open!"<<Endl; the } the //Display off Method + voidTrunoff () - { thecout <<"Monitor off!"<<Endl;Bayi } the }; the - //façade class, shielding customers from the direct access to the above equipment, unified let them through the façade provided by the API to access those devices - classfacade the { the Private: the //Camera theCamera *M_camera; - //Light theLight *m_lights; the //Screen theScreen *M_screen;94 the Public: the //Initialize the facade ()98 { AboutM_camera =NewCamera (); -M_lights =NewLight ();101M_screen =NewScreen ();102 }103 104~facade () the {106 Delete M_camera;107 Delete m_lights;108 Delete m_screen;109 } the //ways to activate your device111 voidActivate () the {113cout <<"Activate the device to start live!"<<Endl; the //turn on the camera theM_camera->Trunon (); the //Adjust camera angle117M_camera->rotate ( -);118 //turn on the lights119M_lights->Trunon (); - //turn on the screen121M_screen->Trunon ();122 123 }124 //ways to turn off all devices the voidDeactivate ()126 {127cout <<"turn off the device!"<<Endl; - //turn off the camera129M_camera->Trunoff (); the //Camera Angle Reset131M_camera->rotate (0); the //Turn off the lights133M_lights->Trunoff ();134 //Turn off the screen135M_screen->Trunoff ();136 }137 };138 139 intMain () $ {141 //because you want to use façade mode, instantiate the façade class142Facade * M_facade =Newfacade ();143 //let customers, unified use of the façade, to unify to dispatch those devices, decoupling144M_facade->Activate ();145cout <<"On the Live!"<<Endl;146M_facade->Deactivate ();147 148 return 0;149}
activating the device to start live !
Camera Open !
Rotate the camera by 90degrees!
Lights Open !
Display Open !
in Live !
turn off the device !
Camera off !
Rotate the camera by 0degrees!
Light off !
Monitor off !
Program ended with exit code:0
If you do not use the façade class, each customer will have to directly call the device, which makes the client's code is very complex, but also make the device directly exposed! and poor readability.
Common design patterns of software development--a summary of façade patterns