Factory method pattern

In computer programming, the factory method pattern is a software design pattern. The factory method pattern is useful to solve the common problem of constructing objects based on some input such that functions inside the objects depend upon the input.

Contents

Example

Consider as an example a class to read image files and make thumbnails out of them. This can be solved by bundling them all into one class and deciding which bit of code to run:

A bad solution

public class ImageReader {
    private int fileType;
    private String fileContents;
    private byte[] decodedImage;

    public ImageReader( InputStream in ) {
        // Figure out what type of file this input stream represents
        // (eg gif, jpeg, png, tif, etc )

        this.fileType = fileType;
        decodeFile();
    }

    private void decodeFile() {
        switch( fileType ) {
        case ImageReader.GIF:
            // do gif decoding (many lines)
            break;
        case ImageReader.JPEG:
            // do jpeg decoding (many lines)
            break;
        case ImageReader.PNG:
            // do png decoding (many lines)
            break;
        // etc...
        }
    }
}

This method has the advantage of abstracting the file type from the class that calls this ImageReader, but as the number of file types supported gets larger, the code will quickly become huge, unwieldy and hard to maintain.

A better, but not perfect, solution

Another solution that seems possible is to have a separate object for each of these file types:

public interface ImageReader {
    public DecodedImage 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
        // decode it.
    }

    public DecodedImage getDecodedImage() {
       return decodedImage;
    }
}

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 = new GifReader( fileInputStream );
        }

        if( endsInDotJpeg( filename )) {
            out = new JpegReader( fileInputStream );
        }

        printOut( out.getDecodedImage );
    }
}

Again, there are advantages to this method (clean organisation of the ImageReader classes, the code being split up in several classes), but this is at the cost of the abstraction of the image type. Again, when dozens of file types need to be supported, this will be unsatisfactory and produce code that is hard to maintain.

Using the factory pattern

The factory pattern can be used instead to combine the better parts of the above solutions. 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 );

        switch( imageType ) {
        case ImageReaderFactory.GIF:
            GifReader r = new GifReader( is );
            return (ImageReader)r;
            break;
        case ImageReaderFactory.JPEG:
            JpegReader r = new JpegReader( is );
            return (ImageReader)r;
            break;
        // etc.
        }
    }
}

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

External links

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools