Java design Patterns and the role of façade patterns in Java

Source: Internet
Author: User

The façade pattern is used in multiple places in Tomcat and is used in the request and response object encapsulation, from ApplicationContext to ServletContext Encapsulation.

A system can have several façade classes

In façade mode, typically only one façade class is required, and this façade class has only one instance, in other words, it is a singleton class. Of course this does not mean that there is only one façade class throughout the system, but only one façade class for each subsystem. or, If a system has several subsystems, each subsystem has a façade class, the whole system can have a number of façade classes.

Add new behavior to subsystems

Beginners often assume that it is wrong to add new behavior to a subsystem by inheriting a façade class. The façade mode is intended to provide a centralized and streamlined communication pipeline for the subsystem, rather than adding new behavior to the Subsystem. For example, a receptionist in a hospital is not a paramedic, and a receptionist cannot provide medical care for a Patient.

Advantages of Façade mode

Advantages of the façade mode:

Loosely coupled

  The façade mode is loosely coupled between the client and subsystem, allowing the modules inside the subsystem to be more easily extended and Maintained.

  Simple to use

  The façade mode makes the subsystem more easy to use, the client no longer needs to understand the implementation within the subsystem, and does not need to interact with the modules inside the many subsystems, just interact with the façade class.

  Better partitioning of access levels

  By using facade wisely, you can help us to better classify the level of access. Some methods are external to the system, and some methods are used internally by the System. The ability to expose external functionality to the façade, which makes it easy for clients to use and hide internal details Well.

The use of façade mode in Tomcat

There are a lot of façade patterns in Tomcat because there are many different components in tomcat, each of which communicates with each other, but does not expose its own internal data too much to other components. It's a good way to isolate data in a façade mode.

Here is the façade mode used on request:

  

Anyone who has used a servlet knows that, in addition to having a corresponding configuration in Web. xml, it is necessary to inherit an abstract class called httpservlet, and rewrite the Doget and Dopost methods (and, of course, just rewrite the service method).

Many of the methods in the request object are used when the internal components interact with each other, such as setcomet, setrequestedsessionid, etc. (not listed here). These methods are not exposed externally, but must be set to public because they also need to be used interactively with internal components. The best solution is to use a facade class that masks the methods used to interact with the internal components and only provides methods of interest to external programs.

If you do not use the facade class, the request object and the response object are passed Directly. Programmers familiar with the internal workings of the container can then convert ServletRequest and Servletresponse objects down to request and response, and invoke their public methods. For example, the request object, you can call setcomet, Setrequestedsessionid and other methods, which will jeopardize Security.

Original link http://www.cnblogs.com/java-my-life/archive/2012/05/02/2478101.html

Java design Patterns and the role of façade patterns in Java

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.