Design pattern of Zen design mode-façade mode

Source: Internet
Author: User

One: the definition of the façade pattern
---> Façade mode (facade pattern) is also called appearance mode, which is a more commonly used encapsulation mode.
---> requires that the outside of a subsystem and its internal communication be carried out through a unified object. Façade mode provides a high-level interface that makes subsystems easier to use.
---> Façade mode focuses on "unified objects", which is the interface that provides an access subsystem, except that this interface does not allow the behavior of any access subsystem
---> This is exactly the pattern we need to design, not to change the external exposed interface, method of the subsystem, only change the internal processing logic, the other sibling module calls produced different results, really is a very good design. This is the façade pattern.


II: The role of façade mode
Facade Façade role
The client can invoke the method of this role. This role knows all the functions and responsibilities of the subsystem. In general, this role delegates all requests from the client to the appropriate subsystem, saying that the role has no actual business logic, just a delegate class.
subsystem Subsystem Role
You can have one or more subsystems at the same time. Each subsystem is not a separate class, but a collection of classes. The subsystem does not know the presence of the façade. For subsystems, the façade is just another client.

Three: The application of façade mode

Façade mode has the following advantages
Reduce system interdependencies
Think about, if we do not use the façade mode, the external access directly into the subsystem inside, is a strong coupling between the relationship, you die I die, you live I can live, such a strong reliance is not acceptable to the system design, the appearance of the façade pattern is a good solution to the problem, all the dependence is on the façade object of dependence, independent of the subsystem.
Increased flexibility
The dependence is reduced, and the flexibility naturally increases. Regardless of how the internal subsystem changes, as long as it does not affect the façade object, let you free movement.
Improve security
You do not have access to any of the methods that you want to access to your subsystem to open up the logic that is not open on the façade.

Façade mode has the following disadvantages
The biggest drawback of the façade mode is that it does not conform to the opening and closing principle, to modify the closed, open to the expansion, look at our door object bar, it is the most important, once the system started to find a small error, how do you solve? Fully comply with the opening and shutting principle, there is no way to solve. Inheritance, overwrite? are not used, the only thing to do is to modify the appearance of the role of the code, the risk is quite large, which requires you in the design of caution, think a few times will have a good harvest.

Usage scenarios for façade mode
Provides an interface for external access to a complex module or subsystem
subsystem is relatively independent--the external access to the subsystem as long as the black box operation can
For example, the calculation of interest, no deep business knowledge and solid technical level is not possible to develop the subsystem, but for the use of the system developers, he needs to do is to enter the amount and the deposit period, the other do not care, the return of the result is interest, this time, the façade mode is not used.
Prevention of risk spread by low-level personnel
For example, a low level of technical personnel involved in project development, in order to reduce the personal code quality of the overall project impact risk, the general practice is "restricting", can only be developed in the specified subsystem, and then provide a façade interface for access operations.

Four: the façade mode considerations
---> A subsystem can have multiple facades
The façade is too big to bear.
For example, a pure façade object has more than 200 lines of code, although it is a very simple delegate operation, it is recommended to split into multiple facades, otherwise it will provide future maintenance and expansion of unnecessary trouble. How do you split it? According to function splitting is a very good principle, such as a database operation of the façade can be split into the query façade, delete the façade, update the façade and so on.
Subsystems can provide different access paths
If the a role, the B role External system access façade c,a role has access to the façade C all methods, but the B role only accesses the façade C part of the method. You need to create a new façade d for the B role to use, and the D façade is just the agent of the façade a,a façade to the d façade to provide B role permissions.
---> Façade does not participate in the business logic within the subsystem
The façade does not participate in the business logic within the subsystem, and if the role that is dependent on the façade has a mutually callable relationship, the role that the project invokes needs to be encapsulated into a new role, aggregated into the façade.

Five: The actual practice of the façade mode
Façade mode is a good encapsulation method, a subsystem is more complex, such as the algorithm or business complex, you can encapsulate one or more façade, the project structure is simple, and extensibility is very good. Also, for a larger project, in order to avoid the risk of personnel, you can also use the façade mode, the technical level than the poor members, as far as possible to arrange a separate module, and then the program he wrote into a façade

Six: Case of façade mode
"1" Letter of Business class

1  PackageCom.yeepay.sxf.template18;2 /**3 * Letter of Business class4 * Hidden in the façade character inside, do not need to expose too much5  * @authorSXF6  *7  */8  Public Interfaceiletterprocess {9     //the contents of the letterTen      Public voidWritecontext (String context); One     //the address of the letter A      Public voidFillenvelope (String address); -     //load a letter into an envelope -      Public voidLetterinotoenvelope (); the     //Send a letter -      Public voidSendletter (); -}
View Code

"2" The implementation of the business class of writing letters

1  PackageCom.yeepay.sxf.template18;2 3  Public classLetterprocessimplImplementsiletterprocess{4 5 @Override6      Public voidWritecontext (String context) {7System.out.println ("Letterprocessimpl.writecontext () wrote the content:" +context);8     }9 Ten @Override One      Public voidFillenvelope (String address) { A         //TODO auto-generated Method Stub -System.out.println ("Letterprocessimpl.fillenvelope () Letter mailing Address:" +address); -     } the  - @Override -      Public voidLetterinotoenvelope () { -System.out.println ("Letterprocessimpl.letterinotoenvelope () put the letter in the envelope"); +     } -  + @Override A      Public voidSendletter () { atSystem.out.println ("letterprocessimpl.sendletter () Mailing Letter"); -     } -  -      -}
View Code

"3" Letter of business class for the façade role

1  PackageCom.yeepay.sxf.template18;2 /**3 * Letter of the façade character4 * One person would like to write a letter, only need to give the façade to provide the corresponding parameters, follow-up events do not care. 5  * @authorSXF6  *7  */8  Public classMODENPOSTOFFCE {9 Ten     Privateiletterprocess letterprocess; One      A      PublicMODENPOSTOFFCE (iletterprocess letterprocess) { -          This. letterprocess=letterprocess; -     } the      -      Public voidSendletter (String context,string address) { -         //Write - Letterprocess.writecontext (context); +         //Write Address - Letterprocess.fillenvelope (address); +         //Pack Envelopes A Letterprocess.letterinotoenvelope (); at         //Send a letter - Letterprocess.sendletter (); -     } -}
View Code

"4" Client test

1  PackageCom.yeepay.sxf.template18;2 /**3 * Client Testing4  * @authorSXF5  *6  */7  Public classClienttest {8 9      Public Static voidMain (string[] args) {Ten         //Letter of Business class OneIletterprocess letterprocess=NewLetterprocessimpl (); A         //Letter of Business class façade class -MODENPOSTOFFCE modenpostoffce=NewMODENPOSTOFFCE (letterprocess); -         //a person writes through the façade theModenpostoffce.sendletter ("Dddsdfdf", "CCCCCCCCCC"); -     } -}
View Code

Design pattern of Zen design mode-façade mode

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.