CSC/ECE 517 Fall 2012/ch2b 2w60 ns: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 77: Line 77:
For Java and C# resources, we recommend:
For Java and C# resources, we recommend:
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]
*[https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf Head First Design Patterns]
*[http://sourcemaking.com/design_patterns/decorator SourceMaking]
*[http://sourcemaking.com/design_patterns/factrory_method/c%2523 SourceMaking]
*[http://blog.image0.com/c-2/design-patterns-series-the-decorator-pattern-with-a-real-world-soccer-example/ blog.image0]
*[http://javarevisited.blogspot.com/2011/12/factory-design-pattern-java-example.html/ javablog]
*[http://www.oodesign.com/decorator-pattern-gui-example-java-sourcecode.html OODesign]
*[http://www.oodesign.com/factory-method-pattern.html OODesign]


==Directory==
==Directory==

Revision as of 02:39, 18 November 2012

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

For a general overview and UML diagrams of the Factory method design pattern, we recommend:


Ruby

Summary

Java & C#

There are a variety of resources available for learning how to use the Factory method pattern in Java and C#. We group these together as they are syntactically similar languages. One of the most popular references for learning about Design Patterns in Java is the book "Head First Design Patterns"<ref name="head_first"> https://docs.google.com/viewer?url=http%3A%2F%2Foreilly.com%2Fcatalog%2Fhfdesignpat%2Fchapter%2Fch03.pdf </ref>; in this book an in-depth explanation of the Factory method Pattern can be found. This is probably more popular because of how in-depth its explanation is of the design pattern, using diagrams and an ongoing coding example.

If you are interested in learning about the Factory method pattern for Java and/or C# but do not have enough time or energy to read the chapter in the "Head First Design Patterns"<ref name="head_first"/> book, there are other resources that can be used to achieve an understanding but are less textual. Most of the resources that are not highly verbose use an over-arching example to explain the pattern. A good and simple resource for learning how to use the Decorator pattern in Java can be found on the site SourceMaking, a site devoted to teaching IT concepts such as Design Patterns and UML to professional developers<ref name="sourcemaking"> http://sourcemaking.com/design_patterns/factrory_method/c%2523 - 2012?</ref>. This resource includes a number of Java (and C#) examples meant to explain the usage of the pattern as well as textual explanations if needed. An advantage to using this resource is that it uses less text and more examples to show how the Factory method pattern can be used. It is also helpful that this site has multiple examples that can help increase understanding through reinforcement and multiple viewpoints (and languages). A disadvantage to using this site is that all of the examples are general examples using the pattern as opposed to real life situations or scenarios.

Some sites use over-arching real life examples to explain the Factory method pattern and its usage. One that seems to be useful in conveying the idea behind the Factory method pattern is using the pattern to show the behaviour of pets <ref name="pets">http://javapapers.com/design-patterns/factory-method-pattern/ - 2012 </ref>. This site uses a mix of textual explanations and examples to explain the pattern. One advantage to using this site is that it is relatively short and includes a real world example. It also provides the code in a way that would make it simple to attempt to run it yourself and see how it works. A disadvantage for this resource is that it does not tell how to approach a "problem" or how to solve complex problem. This is useful to understand the basics for people who are new to design patterns and not useful for developers.

Although most examples for this pattern focus on other usages, it is possible to use the Factory method pattern with GUIs (Graphical User Interfaces) i.e creating Desktop applications. A useful example for understanding this usage can be found on OODesign.com <ref name="gui">http://www.oodesign.com/factory-method-pattern.html- ?</ref>. This site uses code and UML diagrams to explain the usage of Factory method when implementing GUIs. Along with code, there are sections which describe the UML diagrams and drawbacks. This site is useful because it offers code that can easily be tested and/or modified if needed to gain a better understanding. Another advantage is that there is not much that has to be read. A disadvantage is that if the code given is not similar to what you are trying to achieve it may be difficult to understand whether you need to be using the Factory method pattern for you particular GUI; outside sources may be needed for this understanding.

Summary

For Java and C# resources, we recommend:

Directory

References

<references />