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]
# 1

> 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());

> }

> }

> }

>

>

k_i_shor at 2007-7-7 13:16:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

> 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.

%

duffymo at 2007-7-7 13:16:02 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Actually, I just thought of a reason why you'd prefer to throw the exception in the open and not the ctor:

I can envision a scenario where you might create a TextReader using a file that didn't exist when you call the ctor. Your app might then create the file to be read, THEN call the open on the TextReader. You wouldn't want to preclude this possibility by throwing the exception in the ctor. Doing it that way would force clients to create the file first. That might not be such a terrible thing, but you'd have to document it in the javadocs to avoid surprises.

%

duffymo at 2007-7-7 13:16:02 > top of Java-index,Other Topics,Patterns & OO Design...