Concept of basic concept facade
Provides a unified interface for a set of interfaces in a subsystem. The facade mode defines a higher-level interface, making the subsystem easier to use.
- We need to interact with the system in a way that is simpler and more straightforward than the original, and we use only a subset of it for the complex systems we discuss
- Because facade mode can encapsulate multiple subsystems, You can reduce the number of objects that customers must process
- Requires that the external and internal communication of a subsystem be carried out through a unified object . In other words, the facade object is the only channel outside the subsystem, can be used to integrate the internal chaos subsystem, whether or not veneer, can do respectability
The role division of Facade model
facade Façade Object :
- This role knows all the functions and responsibilities of the subsystem .
- In general, this role will delegate all requests from the client to the appropriate subsystem, that is, the role has no actual business logic, just a delegate class .
- Façade objects generally do not participate in the implementation of the business logic within the subsystem . (provides only one path to an access subsystem)
- As an external interface, after the system is put into operation, it should not be modified to hide and encapsulate the subsystem.
The meaning of the encapsulation subsystem:
- Tracking System usage : By forcing all access to the system to be facade, it is easy to monitor system usage
- Change system : The future may need to change the system. By using the original system as a private member of the facade class, you can switch to the new system with the least effort
Sub Systems Subsystem Roles :
- is a collection of classes, but can be independent of each other and there is no interdependence between the systems
- The presence of the façade object is not known.
Example facade
For example, we usually use the computer, the Desktop file shortcut ( or an executable file ), we need to do is just open and close, usually do not need to consider what to open ah, set what parameters Ah, How to destroy resources when shutting down to ensure that the program's data can not cause loss .... This executable file can be seen as a façade . Because it greatly simplifies our operations, and we can completely ignore the interaction between the subsystems , and simply think that I am interacting with a complete system .
The following provides an implementation of an open file operation to illustrate what the façade pattern is:
Class openword{BooleanIsOpen; String name ="Viewer"; Openword () {IsOpen =false; }voidOpen () {System.out.println ("the"+ name +"is opening ...."); IsOpen =true; }voidStop () {System.out.println ("the"+ name +"is closing ...."); IsOpen =false; }}class preparefilesystem{BooleanIsOpen; String name ="FileSystem"; Preparefilesystem () {IsOpen =false; }voidOpen1 () {System.out.println ("the"+ name +"is opening ...."); IsOpen =true; }voidStop1 () {System.out.println ("the"+ name +"is closing ...."); IsOpen =false; }}class operatefile{voidShowfile (String name) {System.out.println ("Show The content of the file:"+ name); }}
All of these are to achieve facade, so do not care too much, as a function to provide the interface class.
Public class openfilefacade { //Must be a private member, otherwise the subsystem will be exposed to an external PrivateOpenword Openword =NewOpenword ();PrivatePreparefilesystem Preparefilesystem =NewPreparefilesystem ();PrivateOperatefile Operatefile =NewOperatefile ();//provide only one access path and do not implement business logic Public void OpenFile(String name) {preparefilesystem.open1 (); Openword.open (); Operatefile.showfile (name); } Public void CloseFile(String name) {openword.stop (); PREPAREFILESYSTEM.STOP1 (); } Public Static void Main(String [] args) {Openfilefacade A =NewOpenfilefacade (); A.openfile ("CookBook"); A.closefile ("CookBook"); }}
Facade Application Scenarios
- Provides a common external access interface for a complex module or subsystem
- subsystem is relatively independent, external access to the subsystem as long as the black box operation can.
- Prevention of risk spread by low-level personnel (to address the horizontal gap in project developers )
- Reduce the risk of personal code quality impact on the entire project, the general practice is to restricting , only in the specified subsystem development, and then provide the façade interface for access.
- Different façade classes can be implemented for the same set of subsystems to facilitate the use of subsystems by different developers and to reduce the cost and risk of interface communication .
Use of the façade mode in tomcat
- HttpRequest and HttpResponse both implement their own facade classes:
- Httprequestfacade
- Httpresponsefacade
- In Httprequestfacade, for example, the accesses collected through it are delegated to HttpRequest, usually encapsulated objects are set to private or protected to prevent direct access in facade.
Java design pattern (iv) facade (façade) mode and tomcat extension