CSC/ECE 517 Fall 2012/ch2b 2w36 av

From Expertiza_Wiki
Revision as of 23:05, 16 November 2012 by Vcorrei (talk | contribs)
Jump to navigation Jump to search

Factory Method pattern and the related patterns (Template, Prototype)

Factory Method

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="web">http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf
</ref>

The new operator is used in languages like Java to create an object. However in this case the object creation details are not encapsulated. Factory method allows a client to request for an object so that the object creation is encapsulated. Factory method uses inheritance for object creation. The superclass has ‘placeholders’ for the steps involved in object creation. The actual details of the object creation are specified in the subclass.[4]

Use of factory method pattern makes the code more flexible to change. In factory method pattern, interface is used to create an object and the actual instantiation of objects is deferred to subclasses that implement this interface. New classes can be added that implement this interface. In this way it becomes easy to add new concrete classes with minimal changes to the classes that use these objects.[head_first_design_pattern]. Factory method can be used when a class does not know the type of objects that it needs to create[7]

UML Diagram for Factory Method Pattern

UML Diagram for factory method pattern

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 [4]

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.

Template Pattern

Template method pattern is used to specify the format of an algorithm. The basic skeleton of an algorithm is defined in the base class using abstract operations. Subclasses override these abstract operations in order to provide concrete behaviour. In this way, two different subclasses may have different implementations of the abstract functions, but the overall structure of the algorithm will remain same[8].Template method pattern is a behavioural design pattern[9]. Template method pattern helps to avoid code duplication and aids in code reuse[10]

UML Diagram for Template Method Pattern

UML Diagram for factory method pattern

In the UML diagram on the right, abstract primitive operations are defined for which the subclasses provide the implementation. Template method is implemented which specifies the structure of the algorithm. The primitive operations are called within this template method. The ConcreteClass provides subclass specific implementation for the primitive operations.

Code Example

This example has been taken from [10]

public abstract class TextDocument {         
    public final void printPage () {
      Document document = createDocument();
      document.printTextHeader();
      System.out.println(document.body());
      document.printTextFooter();
    }
    public abstract Document createDocument();
    public abstract void printTextHeader();
    public abstract void printTextFooter();
    ...
}
public class PlainTextDocumentBuilder extends TextDocument {
    public PlainTextDocumentBuilder(InputStream in){
    //check that it is an Plain text document or throw error
    }
    public Document createDocument()
    {
     return new PlainTextDocument();
     }	
    public void printTextHeader () {
      // Code for header plain text header here.
    }
    public void printTextFooter () {
      // Code for header plain text footer here.
    }
    ...
}
public class HtmlTextDocumentBuilder extends TextDocument {
    public HtmlTextDocumentBuilder(InputStream in){
    //check that it is an HTML text document or throw error
    }
    public Document createDocument()
    {
     return new HtmlTextDocument();
     }	
    public void printTextHeader () {
      // Code for header HTML text header here.
    }
    public void printTextFooter () {
      // Code for header HTML text footer here.
    }
    ...
}

In the above example, two types of text documents are processed, plain text Document and HTML text document. To print any of the document, the procedure to print the document body is the same. However the methods to print the header and the footer are different for both the documents. Therefore they are defined in the abstract class but the specialized implementation is provided in the subclass. printPage() is the template method. Two types of objects are used, PlainTextDocument and HtmlTextDocument. A Factory method createDocument() is called within the Template method printPage() for this purpose.

Prototype Design Pattern

The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it deals with as long as the concrete prototype extends or implements the prototype interface or class. The concrete prototype object is responsible for cloning itself and hence returning the cloned object. The pattern thus enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at run-time as long as they are similar to the abstract prototype [11]. This pattern allows you to avoid expensive initialization routines when you construct objects that are very similar. Also the goal is to minimize the amount of work needed in creating new objects when the initialization routines are expensive. For example, if the initialization routine requires database queries, file look ups, or service calls and you already have other objects in the system that are very similar to the object you are constructing, then the prototype pattern comes into picture and helps you avoid those expensive initializations.

References

<references />