JAVA and mode 13th-facade Mode

Source: Internet
Author: User

The facade mode is the object structure mode. External communication with a sub-system must be performed through a unified facade object. The facade mode provides a high-level interface to make subsystems easier to use.
________________________________________
 
Hospital example
Modern software systems are complex. A common method for designers to process complex systems is to divide them into several smaller subsystems. If a hospital is used as a sub-system, the system can be divided into registration, outpatient service, price assignment, testing, billing, and drug taking according to the Department's functions. It is not easy for a patient to deal with these departments, just as the client of a sub-system deals with various categories of a sub-system.
First, the patient must register first and then go to the clinic. If the doctor asks for a test, the patient must first make a price and then pay the fee before going to the test department for a test. After the test, return to the clinic.
 

Describes the patient experience in the hospital. The box in the figure represents the hospital.
The method to solve this inconvenience is to introduce the facade mode. The hospital can set up a receptionist's location, where the receptionist is responsible for registration, price calculation, payment, and medicine. This receptionist is the embodiment of the facade model. The patient only contacts the receptionist, who deals with various departments.
 
Facade mode structure
The facade mode does not have a general class graph description. The best description method is actually an example.
 
 
Because the structure of the facade mode is too abstract, it is slightly specific. Assume that the subsystem has three modules: ModuleA, ModuleB, and ModuleC. They have an example method. The overall structure of the example is as follows:
 


In this object diagram, two roles are displayed:
● Facade role: the client can call this role method. This role is aware of the functions and responsibilities of one or more subsystems. Under normal circumstances, this role delegates all requests sent from the client to the corresponding 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 above subsystem is composed of three classes: ModuleA, ModuleB, and ModuleC ). Each subsystem can be called directly by the client or by the facade role. The subsystem does not know the existence of the facade. For the subsystem, the facade is just another client.
Source code
Classes in subsystem roles:
[Java]
1. package com. bankht. Facade;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:16:52
6 .*
7. * @ Class description: Class in the subsystem role:
8 .*/
9. public class ModuleA {
10. // schematic method
11. public void testA (){
12. System. out. println ("Call testA method in ModuleA ");
13 .}
14 .}
 

 
[Java]
1. package com. bankht. Facade;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:17:23
6 .*
7. * @ Class description:
8 .*/
9. public class ModuleB {
10. // schematic method
11. public void testB (){
12. System. out. println ("Call the testB method in ModuleB ");
13 .}
14 .}
 

 
[Java]
1. package com. bankht. Facade;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:17:42
6 .*
7. * @ Class description:
8 .*/
9. public class ModuleC {
10. // schematic method
11. public void testC (){
12. System. out. println ("Call testC method in ModuleC ");
13 .}
14 .}
 

Corner class:
[Java]
1. package com. bankht. Facade;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:17:55
6 .*
7. * @ Class description: corner class of the facade:
8 .*/
9. public class Facade {
10. // schematic method to meet the functions required by the client
11. public void test (){
12. ModuleA a = new ModuleA ();
13. a. testA ();
14. ModuleB B = new ModuleB ();
15. B. testB ();
16. ModuleC c = new ModuleC ();
17. c. testC ();
18 .}
19 .}
 

Client angle:
[Java]
1. package com. bankht. Facade;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:18:17
6 .*
7. * @ Class description: client role class:
8 .*/
9. public class Client {
10.
11. public static void main (String [] args ){
12.
13. Facade facade = new Facade ();
14. facade. test ();
15 .}
16.
17 .}
 

 
The Facade class is actually equivalent to the appearance interface of the, B, and C modules. With this Facade class, the client does not need to call the, B, and C modules of the subsystem, you do not need to know the implementation details inside the system, or even the existence of modules A, B, and C. The client only needs to interact with the Facade class, in this way, the decoupling between the client and sub-system modules A, B, and C is better implemented, making the client easier to use the system.
 
________________________________________
 
Facade mode implementation
Another advantage of using the facade mode is that the method can be selectively exposed. The methods defined in a module can be divided into two parts, one for external use of the subsystem, and the other for mutual calls between modules of the subsystem. With the Facade class, the methods used to call each other between internal modules of the subsystem do not need to be exposed to the external modules of the subsystem.
For example, the following modules are defined: A, B, and C.
[Java]
1. package com. bankht. Facade. SelectiveExposure;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:20:48
6 .*
7. * @ Class description:
8 .*/
9. public class ModuleA {
10 ./**
11. * methods provided for external use of subsystems
12 .*/
13. public void a1 (){
14. System. out. println ("ModuleA. a1 ()");
15 .};
16.
17 ./**
18. * methods used for inter-system inter-module calls
19 .*/
20. public void a2 (){
21 .};
22.
23. public void a3 (){
24 .};
25 .}
 

 
[Java]
1. package com. bankht. Facade. SelectiveExposure;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:21:04
6 .*
7. * @ Class description:
8 .*/
9. public class ModuleB {
10 ./**
11. * methods provided for external use of subsystems
12 .*/
13. public void b1 (){
14. System. out. println ("ModuleB. b1 ()");
15 .};
16.
17 ./**
18. * methods used for inter-system inter-module calls
19 .*/
20. public void b2 (){
21 .};
22.
23. public void b3 (){
24 .};
25 .}
 

 
[Java]
1. package com. bankht. Facade. SelectiveExposure;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:21:20
6 .*
7. * @ Class description:
8 .*/
9. public class ModuleC {
10 ./**
11. * methods provided for external use of subsystems
12 .*/
13. public void c1 (){
14. System. out. println ("ModuleC. c1 ()");
15 .};
16.
17 ./**
18. * methods used for inter-system inter-module calls
19 .*/
20. public void c2 (){
21 .};
22.
23. public void c3 (){
24 .};
25 .}
 

 
[Java]
1. package com. bankht. Facade. SelectiveExposure;
2.
3 ./**
4. * @ author: special troops-AK47
5. * @ Creation Time: 04:21:35
6 .*
7. * @ Class description:
8 .*/
9. public class ModuleFacade {
10.
11. private ModuleA a = new ModuleA ();
12. private ModuleB B = new ModuleB ();
13. private ModuleC c = new ModuleC ();
14.
15 ./**
16. * The following are the methods provided by modules A, B, and C for the external system.
17 .*/
18. public void a1 (){
19. a. a1 ();
20 .}
21.
22. public void b1 (){
23. B. b1 ();
24 .}
25.
26. public void c1 (){
27. c. c1 ();
28 .}
29 .}
 

Client class:
[Java]
1. package com. bankht. Facade. SelectiveExposure;
2 ./**
3. * @ author: Special Forces-AK47
4. * @ Creation Time: 04:24:12
5 .*
6. * @ Class description:
7 .*/
8. public class Client {
9.
10. public static void main (String [] args ){
11. ModuleFacade facade = new ModuleFacade ();
12. facade. a1 ();
13. facade. b1 ();
14. facade. c1 ();
15 .}
16 .}
 

 
Defining a ModuleFacade class effectively shields internal details, so that the client does not need to know some methods when calling the Module class. For example, the a2 () and a3 () methods do not need to be known to the client. Otherwise, internal details are exposed, and the client is confused. For the client, he may have to think about what the a2 () and a3 () methods are used? In fact, the a2 () and a3 () Methods interact with internal modules. Originally, they are not external to the subsystem, so do not let the client know.
A system can have several portals.
In facade mode, only one facade class is required, and this facade class has only one instance. In other words, it is a singleton class. Of course, this does not mean that there is only one portal class in the entire system, but only one portal class for each subsystem. In other words, if a system has several subsystems, each subsystem has a portal class, and the entire system can have several portals.
Add new behaviors to subsystems
Beginners often think that they can add new behaviors to the subsystem by inheriting a facade class. This is wrong. The facade model is intended to provide a centralized and simplified communication channel for the subsystem, rather than adding new behaviors to the subsystem. For example, the receptionist in the hospital is not a medical specialist, and the receptionist cannot provide medical services for the patient.
 
Advantages of Facade Mode
Advantages of the facade mode:
● Loose coupling
The facade mode loose the coupling relationship between the client and subsystem, making it easier to expand and maintain modules within the subsystem.
● Easy to use
The facade mode makes the subsystem easier to use. The client no longer needs to understand the internal implementation of the subsystem, nor need to interact with the internal modules of many subsystems. It only needs to interact with the portal class.
● Better access levels
The rational use of Facade can help us better classify access layers. Some methods are external to the system, and some methods are used inside the system. The functions that need to be exposed to the external are concentrated in the facade, which facilitates the use of the client and hides the internal details.
      
________________________________________
 
Use of Facade mode in Tomcat
Tomcat uses many facade modes, because there are many different components in Tomcat, each component needs to communicate with each other, but it cannot expose too much internal data to other components. It is a good method to isolate data in the facade mode.
The following is the facade mode used on the Request:
 
 

 

Anyone who has used Servlet knows that, except for the web. in addition to configuring xml, an abstract class called HttpServlet must be inherited and the doGet and doPost methods must be rewritten (of course, only the service method can be rewritten ).
[Java]
1. public class TestServlet extends HttpServlet {
2.
3. public void doGet (HttpServletRequest request, HttpServletResponse response)
4. throws ServletException, IOException {
5.
6. this. doPost (request, response );
7.
8 .}
9.
10. public void doPost (HttpServletRequest request, HttpServletResponse response)
11. throws ServletException, IOException {
12.
13.
14 .}
15.
16 .}
 

 
We can see that the doGet and doPost methods have two parameters: the interface HttpServletRequest and the interface HttpServletResponse. So what is the actual type passed from Tomcat? Through debug, we can find that many Tomcat methods are used before the TestServlet class is actually called. As shown in
 


Note the class in the red circle. The 225 line code of the invoke method in the StandardWrapperValue class is as follows:
[Java]
1. filterChain. doFilter
2. (request. getRequest (), response. getResponse ());
 

 
In the StandardWrapperValue class, the Request object and Response object are not directly passed to the doFilter method of the ApplicationFilterChain class. The RequestFacade and ResponseFacade objects are passed. Why? Let's take a look at the request. getRequest () and response. the getResponse () method is clear.
Request class
[Java]
1. public HttpServletRequest getRequest (){
2. if (facade = null ){
3. facade = new RequestFacade (this );
4 .}
5. return facade;
6 .}
 

Response class
[Java]
1. public HttpServletResponse getResponse (){
2. if (facade = null ){
3. facade = new ResponseFacade (this );
4 .}
5. return (facade );
6 .}
 

 
We can see that they all return their own portals. What are the advantages of doing so?
Many methods in the Request object are used when internal components interact with each other, such as setComet and setRequestedSessionId (not listed here ). These methods are not made public to the outside, but must be set to public, because they also need to interact with internal components. The best solution is to use a Facade class to shield the methods used for interaction with internal components and only provide methods that are of interest to external programs.
If the Facade class is not used and the Request object and Response object are directly transmitted, programmers familiar with the internal operation of the container can respectively convert ServletRequest and ServletResponse objects to Request and Response, and call their public methods. For example, if you have a Request object, you can call methods such as setComet and setRequestedSessionId, which threatens security.

Author: m13666425773
 

 


Related Article

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.