CSC/ECE 517 Fall 2012/ch2b 2w60 ns

From Expertiza_Wiki
Revision as of 18:25, 17 November 2012 by Nartal (talk | contribs)
Jump to navigation Jump to search

Factory Method

General Resources

Overview of the Pattern

Factory method pattern is an Object Oriented Programming concept in which objects can be created without specifying the class to which they belong.Factory method pattern implement the concept of using an object to generate other objects.<ref name="web">http://www.oodesign.com/factory-pattern.html
</ref>Factory method is thus a type of creational pattern. An interface is defined for object creation. However the subclasses decide which class they want to instantiate. Factory methods thus abstract object instantiation from the client.<ref name="userpagesfactory">http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf
</ref>


Wikipedia<ref name="wikipedia">http://en.wikipedia.org/wiki/Factory_method_pattern</ref> and dofactory<ref name="dofactory">http://www.dofactory.com/Patterns/PatternFactory.aspx#UML</ref> provide good explanations of the Factory Method Pattern. dofactory<ref name="dofactory"/> is much more concise than Wikipedia<ref name="wikipedia"/>. It is a good resource for quickly viewing the definition and participants of the pattern. Wikipedia<ref name="wikipedia"/>, on the other hand, is much more detailed, it goes into the motivation for the pattern and discusses use of the patterns in several languages such as Java and C# at a high level.

javapapers<ref name="javapapers">http://javapapers.com/design-patterns/factory-method-pattern/</ref> is a good page that gives a basic overview of the pattern through the presentation of a concrete example in java language. There are other Java resources and resources for other languages that are similar below.

UML

Understand the UML diagram for the Factory method pattern can help you understand the purpose of the Factory method pattern, the classes involved and how they interact with each other. UML is not programming language specific, so you should be able look at the UML diagram in any of the resources that have them on this page, regardless of what languages you know.

UML Diagram for factory method pattern <ref name="userpagesfactory" />

In the UML diagram on the right, Product is the interface for the type of object created by the factory method. The Product interface is implemented by ConcreteProduct. The factory method which returns an object of type Product, is declared in Creator. The factory method is overridden inside ConcreteCreator to return an instance of ConcreteProduct.









Code Example

This example has been taken from <ref name="sourcemakingfact"> http://sourcemaking.com/design_patterns/factory_method/</ref>

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 {
   //...
}

In the example above the ImageReader interface is defined. In this interface, a method getDecodedImage is defined that should return a decoded image. However this image may have been encoded in any format for example GIFF or JPEG. Therefore the functionality of actually decoding the image is implemented inside two classes GifReader and JpegReader . In this way, the responsibility of creating objects decoded in the appropriate format has been delegated to the subclass implementing the interface.


Summary

Ruby

Summary

Java & C#

Summary

Directory

References

<references />