Decorator Patterns InputStream and OutputStream in the Java SDK

Decorator pattern is a structural design pattern that dynamically adds functionality without changing existing objects. The InputStream and OutputStream in the Java SDK use the decorator pattern. InputStream and OutputStream are abstract classes that provide basic support for I/O operations. The decorator pattern extends its functionality by adding decorator classes to the basic input and output streams. In the Java SDK, the complete source code for the decorator mode is as follows: ```java //Abstract Basic Class public abstract class InputStream { public abstract int read() throws IOException; } //Specific implementation classes of basic classes public class FileInputStream extends InputStream { //Read File Content @Override public int read() throws IOException { //Specific reading implementation } } //Abstract Decorator Class public abstract class FilterInputStream extends InputStream { protected volatile InputStream in; protected FilterInputStream(InputStream in) { this.in = in; } } //The specific implementation class of the decorator class public class BufferedInputStream extends FilterInputStream { //Add buffering function public int read() throws IOException { //Specific reading implementation, including buffering function } } //Client code public class Client { public static void main(String[] args) throws IOException { InputStream inputStream = new FileInputStream("file.txt"); InputStream bufferedInputStream = new BufferedInputStream(inputStream); //Using decorators to enhance functionality int data = bufferedInputStream.read(); while (data != -1) { //Processing data data = bufferedInputStream.read(); } } } ``` Summary: The decorator pattern is widely used in Java SDK, and through the combination of decorator classes, functions can be flexibly added or removed. It follows the "Open–closed principle", so that objects in the system can be extended without modifying the source code. Using the decorator pattern can achieve a dynamic wrapping mechanism that makes object enhancement and extension very simple and flexible.

Decorator Pattern Filter in the Spring Framework

In the Spring framework, the decorator pattern is widely used in the implementation of Filter. Filter is a component in the Servlet specification used for pre processing and post processing requests and responses. In the Spring framework, Filter adopts the decorator mode to enhance its functionality. Specifically, the Filter in the Spring framework consists of the abstract class OncePerRequestFilter and the implementation class ConcreteFilter, with OncePerRequestFilter serving as the abstract decorator and ConcreteFilter serving as the concrete decorator. OncePerRequestFilter provides an abstract method, doFilterInternal, in which specific filtering logic is defined. This method accepts ServletRequest and ServletResponse objects as parameters and can perform any operation on them. The concrete decorator ConcreteFilter inherits from OncePerRequestFilter and implements the doFilterInternal method. In the implementation, ConcreteFilter can perform some preprocessing operations, then call the doFilterInternal method of the parent class, and finally perform some post-processing operations. The following is the complete source code of the Filter decorator mode in the Spring framework: ```java public abstract class OncePerRequestFilter implements Filter { @Override public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; //Perform some preprocessing operations doFilterInternal(httpRequest, httpResponse, filterChain); //Perform some post-processing operations } protected abstract void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException; //Other methods } public class ConcreteFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { //Specific filtering logic //Calling methods of parent classes for filtering super.doFilterInternal(request, response, filterChain); //Other post-processing operations } } //Other specific filter classes that implement the Filter interface ``` Summary: The decorator pattern has been widely applied in the implementation of Filter in the Spring framework. By using the decorator mode, Filter can enhance its functionality in a flexible way. Specifically, the abstract class OncePerRequestFilter serves as an abstract decorator, defining a universal filtering process, while the concrete decorator ConcreteFilter inherits from the abstract decorator, implementing specific filtering logic and enhancement functions. This design allows for flexible extension of Filter's functionality while maintaining high cohesion and low coupling of the code.