Java---Design Patterns---Decoration

Scenarios and Questions

    How to provide enhanced additional functions to one or more existing class objects without modifying the original object class?

MyBufferedReader:

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * Time:2018/4/15
 * Description:
 * Decorate:
 * Provides enhanced additional functions to one or more existing class objects without modifying the original object class.
 * Learn decoration mode by imitating BufferedReader in API.
 * @author Song Jinyu
 */
public class MyBufferedReader extends Reader {
	private Reader reader; //Enhanced object
	private char[] cbuf = new char[1024];//Indicates the buffer area, the size is 1K
	private int size = 0; //represents the number of characters in the buffer
	private int pos = 0; //indicates where the buffer is currently read
	
	//Since it is to strengthen the function of the existing class, then we must need to receive a class object that needs to be strengthened from the constructor
	public MyBufferedReader( Reader reader) {
		this.reader = reader;
	}
	/**
	 * read a character
	 * @return is a char character represented by int. If it is 0-65535, it means that there is data. If it is -1, it means that there is no data.
	 * @throws IOException
	 */
	public int read() throws IOException{//If an exception needs to be thrown, leave the decision to the caller
		if(size==pos){//If size==pos indicates that the data in the buffer has been read, the data needs to be reloaded
			size = reader.read(cbuf);
			pos=0;//Start reading from the position of buffer 0
		}
		if (size==-1) {
			return -1;
		}
		return cbuf[pos++];
	}
	//Test the read() method in MyBufferedReader
// public static void main(String[] args) throws IOException {//In order to test the code structure clearly, throw a logical exception
//		MyBufferedReader mbr = new MyBufferedReader(new FileReader("a.txt"));
//		int c;
//		while((c=mbr.read())!=-1){
//			System.out.print((char)c);
//		}
//	}
	/**
	 * read a line of characters
	 * @return is a string, which means to read a line of string, if the return value is null, it means that it has been read.
	 */
	public String readLine() throws IOException{
		StringBuilder sb = new StringBuilder();//Because the string is constantly modified, using StringBuilder is faster and saves resources
		int c;
		while((c=read())!=-1){
			char ch = (char)c;//Note that conversion is required
			if (ch=='\r') {//Enter, remove
				continue;
			}
			if (ch=='\n') {
				return sb.toString();
			}
			sb.append(ch);
		}
		if (sb.length()>0) {//Prevent the last line without a newline '\n'
			return sb.toString();
		}
		return null;
	}
	//Test the readLine() method in MyBufferedReader
	public static void main(String[] args) throws IOException {//In order to test the code structure clearly, throw a logical exception
		MyBufferedReader mbr = new MyBufferedReader(new FileReader("a.txt"));
		String str;
		while((str = mbr.readLine())!=null){
			System.out.println(str);
		}
	}
	@Override
	public int read(char[] cbuf, int off, int len) throws IOException {
		return reader.read(cbuf, off, len);
	}
	@Override
	public void close() throws IOException {
		reader.close();
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325388765&siteId=291194637