Factory method pattern: Difference between revisions

Content deleted Content added
add missing void
Kefka (talk | contribs)
m Reformatted the code to agree with Java Coding Conventions
Line 6:
 
 
public class ImageReader {
private int fileType;
{
private intString FileTypefileContents;
private Stringbyte[] FileContentsdecodedImage;
private byte[] DecodedImage;
public ImageReader( InputStream in ) {
// Figure out what type of file this input stream represents
public ImageReader( InputStream in )
// (eg gif, jpeg, png, tif, etc )
{
// Figure out what type of file this input stream represents
this.fileType = fileType;
// (eg gif, jpeg, png, tif, etc )
decodeFile();
 
}
FileType = file_type;
 
private void decodeFile(); {
switch( fileType ) {
}
case ImageReader.GIF:
// do gif decoding (many lines)
private void decodeFile()
break;
{
case ImageReader.JPEG:
switch( FileType )
// do jpeg decoding (many lines)
{
break;
case ImageReader.GIF:
case ImageReader.PNG:
// do gif decoding (many lines)
// do png decoding (many lines)
break;
break;
case ImageReader.JPEG:
// etc...
// do jpeg decoding (many lines)
}
break;
}
case ImageReader.PNG:
// do png decoding (many lines)
break;
// etc...
}
}
}
 
Line 46 ⟶ 41:
Another solution that seems possible is to have a separate [[Object (computer science)|object]] for each of these file types:
 
public Interface ImageReader {
public ImageReader( InputStream in );
{
public getDecodedImage();
public ImageReader( InputStream in );
public getDecodedImage();
}
public class GifReader implements ImageReader {
public GifReader( InputStream in ) {
{
// check that it's a gif, throw exception if it's not, then if it is
public GifReader( InputStream in )
// decode it.
{
}
// check that it's a gif, throw exception if it's not, then if it is
 
// decode it.
public getDecodedImage() {
}
return DecodedImage;
public getDecodedImage()
}
{
return DecodedImage;
}
}
 
public class JpegReader implements ImageReader
public class JpegReader implements ImageReader {
{
//....
}
// Then you would use them as:
public class MyProg {
 
{
public static void main( String[] args ) {
 
{
String filename = args[0];
ImageReader out;
if( endsInDotGif( filename )) {
out = (ImageReader)new GifReader( fileInputStream );
}
 
if( endsIndotgifendsInDotJpeg( filename ) ) {
out = (ImageReader)new JpegReader( fileInputStream );
{
}
out = (ImageReader)new GifReader( file_input_stream );
}
if( endsIndotjpeg( filename ) )
{
out = (ImageReader)new JpegReader( file_input_stream );
}
 
printOut( out.getDecodedImage );
}
}
 
Line 96 ⟶ 87:
So what's the solution ? Simply to take the best of both worlds, by using the Factory pattern (which you should already have an idea of by now). The [[Factory]] is a class that returns another class depending on the [[context]]. So in this situation, keeping the separate [[class design]] as in the last example:
 
public class ImageReaderFactory {
public static ImageReader getImageReader( InputStream is ) {
{
int ImageType = figureOutImageType( is );
public static ImageReader getImageReader( InputStream is )
{
int ImageType = figureOutImageType( is );
 
switch( ImageType ) {
case ImageReaderFactory.GIF:
{
GifReader r = new GifReader( is );
case ImageReaderFactory.GIF:
return( (ImageReader)r );
GifReader r = new GifReader( is );
break;
return( (ImageReader)r );
case ImageReaderFactory.JPEG:
break;
JpegReader r = new JpegReader( is );
case ImageReaderFactory.JPEG:
return( (ImageReader)r );
JpegReader r = new JpegReader( is );
break;
return( (ImageReader)r );
// etc.
break;
}
// etc.
}
}
}