Design 101: Create a TextReader to wrap BufferedReader. Adapter? 3 question
Context: I want to learn more about java API and to know when to apply the appropriate pattern if there the need is justified.
So, I create a wrapper around BufferedReader to create a text file reader. See code 1 and code 2 as usage.
Question:
1. Design-wise, is this wrapper a simple wrapper and nothing else in the sense
it look like an Adapter but it's not from the GoF definition.
2. In my TextReader constructor, I throw an IllegalArgumentException if the file name passed in
is not there. Is IllegalArgumentException necessary in the constructor given my open() will
throw a TextReaderException any way for this case.
3. Code-wise, is there any alternative way of creating this wrapper?
1M Thanks.
1.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException;
publicclass TextReader{
private BufferedReader br;
private String fileName;
/**
* Constructor TextReader
*
*
*/
public TextReader(String fileName){
if (fileName==null || fileName.trim().length()==0)
thrownew IllegalArgumentException(fileName+" is an illegal or inappropriate argument.");
else
this.fileName=fileName;
}
/**
* Method open
*
*
*/
publicvoid open()throws TextReaderException{
try{
br =new BufferedReader(new FileReader(fileName));
}
catch (FileNotFoundException e){
thrownew TextReaderException(e.toString());
}
catch (IOException e){
thrownew TextReaderException(e.toString());
}
}
/**
* Method getLine
*
*
* @return a line
*
*/
public String getLine()throws TextReaderException{
try{
return br.readLine();
}
catch (IOException e){
thrownew TextReaderException(e.toString());
}
}
/**
* Method close
*
*
*/
publicvoid close()throws TextReaderException{
if (br!=null){
try{
br.close();
}
catch (IOException e){
thrownew TextReaderException(e.toString());
}
}
}
}
2.
publicstaticvoid main(String[] args){
TextReader textFile =new TextReader(fileName);
try{
String aLine;
textFile.open();
while ((aLine=textFile.getLine())!=null){
// do something with aLine
}
}
catch (TextReaderException e){
LogManager.getInstance().error(e.toString());
}
finally{
try{
if (textFile!=null)
textFile.close();
}
catch (TextReaderException e){
LogManager.getInstance().error(e.toString());
}
}
}
[5916 byte] By [
PaulPhuoc] at [2007-9-30 22:46:12]

> Context: I want to learn more about java API and to
> know when to apply the appropriate pattern if there
> the need is justified.
>
> So, I create a wrapper around BufferedReader to
> create a text file reader. See code 1 and code 2 as
> usage.
>
> Question:
> 1. Design-wise, is this wrapper a simple wrapper and
> nothing else in the sense
> it look like an Adapter but it's not from the GoF
> definition.
It is not Adaptor pattern. And there is no need for this wrapper as it doesn't do any addtional work more than BufferedReader.
> 2. In my TextReader constructor, I throw an
> IllegalArgumentException if the file name passed in
> is not there. Is IllegalArgumentException necessary
> in the constructor given my open() will
> throw a TextReaderException any way for this case.
It would always be better to throw checked exception from a contructor.
> 3. Code-wise, is there any alternative way of
> creating this wrapper?
>
I don't think wrapper is needed looking at this code.
> 1M Thanks.
>
> 1.
> > import java.io.BufferedReader;
> import java.io.FileReader;
> import java.io.IOException;
> import java.io.FileNotFoundException;
>
> public class TextReader {
> private BufferedReader br;
> private String fileName;
>
> /**
> * Constructor TextReader
> *
> *
> */
> public TextReader(String fileName) {
> if (fileName==null || fileName.trim().length()==0)
> throw new IllegalArgumentException(fileName+" is
> is an illegal or inappropriate argument.");
> else
> this.fileName=fileName;
> }
>
> /**
> * Method open
> *
> *
> */
> public void open() throws TextReaderException{
> try {
> br = new BufferedReader(new
> new FileReader(fileName));
> }
> catch (FileNotFoundException e) {
> throw new TextReaderException(e.toString());
> }
> catch (IOException e) {
> throw new TextReaderException(e.toString());
> }
> }
>
> /**
> * Method getLine
> *
> *
> * @return a line
> *
> */
> public String getLine() throws TextReaderException{
> try {
> return br.readLine();
> }
> catch (IOException e) {
> throw new TextReaderException(e.toString());
> }
> }
>
> /**
> * Method close
> *
> *
> */
> public void close() throws TextReaderException{
> if (br!=null) {
> try {
> br.close();
> }
> catch (IOException e) {
> throw new TextReaderException(e.toString());
> }
> }
> }
> }
>
>
>
> 2.
>
> public static void main(String[] args) {
> TextReader textFile = new TextReader(fileName);
> try {
> String aLine;
> textFile.open();
> while ((aLine=textFile.getLine())!=null) {
> // do something with aLine
> }
> }
> catch (TextReaderException e) {
> LogManager.getInstance().error(e.toString());
> }
> finally {
> try {
> if (textFile!=null)
> textFile.close();
> }
> catch (TextReaderException e) {
> LogManager.getInstance().error(e.toString());
> }
> }
> }
>
>
> Question:
> 1. Design-wise, is this wrapper a simple wrapper and
> nothing else in the sense
> it look like an Adapter but it's not from the GoF
> definition.
I think all the java.io classes are based on Decorator. Give a look to the GZip streams to see what I mean.
> 2. In my TextReader constructor, I throw an
> IllegalArgumentException if the file name passed in
> is not there. Is IllegalArgumentException necessary
> in the constructor given my open() will
> throw a TextReaderException any way for this case.
6 of one, 1/2 dozen of the other, IMO.
%