import java.io.IOException;
import java.io.Reader;
/*
* 用装饰设计模式自己模拟BufferedReader类
* 模拟close()方法,readLine()方法
*/
public class MyBufferedReader {
private Reader r;
public MyBufferedReader(){}
public MyBufferedReader(Reader r) {
this.r = r;
}
//readLine()方法如何模拟呢?
public String myReadLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch=r.read())!=-1){
if(ch=='\r'){
continue;
}
if(ch=='\n'){
return sb.toString();
}
else{
sb.append((char)ch);
}
}
//为了避免数据的丢失
if(sb.length()>0){
return sb.toString();
}
return null;
}
public void myClose() throws IOException{
r.close();
}
}
个人对装饰设计模式的总结:它对程序进行了功能的增强,比继承更灵活些
|
|