Content deleted Content added
m dab object |
rm non-subjective links |
||
Line 1:
A [[software design pattern]] '''factory pattern''' is used for [[computer programming]].
'''See also:''' [[Software design pattern|Design pattern]], [[Abstract factory pattern]], [[ClassFactory]]▼
from [http://www.mode-x.com/index.php?id=24 mode-x.com] (contributed by author):▼
=== Factory design pattern in Java ===
The Factory pattern is useful to solve a common
Line 20 ⟶ 16:
// Figure out what type of file this input stream represents
// (eg gif, jpeg, png, tif, etc )
FileType = file_type;
decodeFile();
}
Line 43 ⟶ 39:
}
}
1 - A bad way to design the ImageReader
This [[method]] has the
Another
public Interface ImageReader
Line 79 ⟶ 76:
String filename = args[0];
ImageReader out;
if( endsIndotgif( filename ) )
{
Line 88 ⟶ 85:
out = (ImageReader)new JpegReader( file_input_stream );
}
printOut( out.getDecodedImage );
}
}
2 - Another better, but still not so good way to read images
Again, there are
So what's the
public class ImageReaderFactory
Line 103 ⟶ 101:
{
int ImageType = figureOutImageType( is );
switch( ImageType )
{
Line 118 ⟶ 116:
}
}
3 - A good design for the ImageReader: The Factory design pattern
Et voilà! That's all there is to it. The Factory pattern, while not so complex as to be awe-inspiring, is a neat
The [[combination]] of a [[type code]] and its associated specific [[factory object]] can be stored in a [[map]]. The [[switch statement]] can be avoided to create an extensible factory by a [[mapping]].
==See also==
* [[Software design pattern|Design pattern]]
▲
==Reference==
|