In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the decorator mode in java". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. basic concepts
Decorator mode, also known as wrapper mode. Allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern that acts as a wrapper around an existing class.
This pattern creates a decorative class that wraps the original class and provides additional functionality while maintaining the integrity of the class method signature.
2. Usage Scenario 2.1 HttpServletRequestWrapper/** * Provides a convenient implementation of the HttpServletRequest interface that * can be subclassed by developers wishing to adapt the request to a Servlet. * This class implements the Wrapper or Decorator pattern. Methods default to * calling through to the wrapped request object. * * @see javax.servlet.http.HttpServletRequest * @since v 2.3 */public class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequest { /** * Constructs a request object wrapping the given request. * * @param request The request to wrap * * @throws java.lang.IllegalArgumentException * if the request is null */ public HttpServletRequestWrapper(HttpServletRequest request) { super(request); }}
HttpServletRequestWrapper is a concrete decorator implementation class that does nothing, but inherits the decorator implementation class and implements HttpServletRequst. It makes sense to do nothing here, because there is no way to know in advance what features to add to the request, and it is impossible to add them blindly. Therefore, the decorative pattern in this application scenario: suitable for the default target implementation is unknown or not easy to extend the situation.
Decorative patterns are the ability to dynamically extend the functionality of an object without changing the original class file and using inheritance. It wraps the real object by creating a wrapper object, or decoration. So in the actual application of the method encountered to enhance the object, in the end choose which way is better? There is no specific formula for this, it can only be used in a specific way according to specific requirements, but there is a case where the Decorator design pattern must be used: that is, the object is enhanced, and the developer can only get its object, not its class file.
For example, request and response objects. The reason why developers can operate these objects in servlets through the HttpServletRequest/Response interface defined by sun company is because Tomcat server manufacturers have written the implementation classes of request and response interfaces. When the web server calls servlets, it creates objects from the implementation classes of these interfaces and passes them to the servlet program. In this case, because the developer does not know which implementation class of the request and response interfaces written by the server vendor is? Only objects provided by the server vendor can be obtained in the program, so these objects can only be enhanced using the Decorator design pattern.
When we use filter, we find that at least half the time we want to change the parameters of the HttpServletRequest object. For example: filter the blank space entered by the user before the HttpServletRequest object reaches the Servlet. But since the parameters of the HttpServletRequest object wrapped in java.util.Map are immutable, what do you do? Fortunately, although we can't change the object itself, we can change its state through decorators. To change the parameters in HttpServletRequest, you can do so through HttpServletRequestWrapper, the decorator class. You just need to rewrite its getParameter(getParameterValues) method as needed in the decorator class.
2.2 ServerWebExchangeDecorator
The decorator used in WebFlux has a similar principle and can modify the request and response parameters (note that modifying directly in the filter will throw an UnsupportedOperationException for security reasons. For specific solutions, please refer to xbuba. com-How to add custom headers to Spring WebFilter?).
/** * A convenient base class for classes that need to wrap another * {@link ServerWebExchange}. Pre-implements all methods by delegating to the * wrapped instance. * *
Note: if the purpose for using a decorator is to override * properties like {@link #getPrincipal()}, consider using * {@link ServerWebExchange#mutate()} instead. * * @author Rossen Stoyanchev * @since 5.0 * * @see ServerWebExchange#mutate() */public class ServerWebExchangeDecorator implements ServerWebExchange { private final ServerWebExchange delegate; protected ServerWebExchangeDecorator(ServerWebExchange delegate) { Assert.notNull(delegate, "ServerWebExchange 'delegate' is required. "); this.delegate = delegate; }}/** * Wraps another {@link ServerHttpRequest} and delegates all methods to it. * Sub-classes can override specific methods selectively. * * @author Rossen Stoyanchev * @since 5.0 */public class ServerHttpRequestDecorator implements ServerHttpRequest { private final ServerHttpRequest delegate; public ServerHttpRequestDecorator(ServerHttpRequest delegate) { Assert.notNull(delegate, "Delegate is required"); this.delegate = delegate; {}"What is the decorator pattern in Java" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.