Factory Method Pattern


Published on

Creational Pattern

Published in: Business
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Factory Method Pattern

  1. 1. Factory Method Pattern<br />CSE776-Design Patterns<br />By<br />Anjan Kumar Bollam<br />Professor Jim Fawcett<br />
  2. 2. Intent<br /><ul><li>“Define an interface for creating an object, but let subclasses decide which class to instantiate”
  3. 3. It lets a class defer instantiation to subclasses at run time.
  4. 4. It refers to the newly created object through a common interface.</li></li></ul><li>Also Known as<br /><ul><li>Virtual Constructor
  5. 5. The main intent of the virtual constructor idiom in C++ is to create a copy of an object or a new object without knowing its concrete type and this is exactly the Factory Method of initialization.</li></li></ul><li>Motivating Examples<br /><ul><li>Hotel: One good example for the Factory Method is the Hotel. When Check in, the person gets the key from the front desk where he can be looked up as the ‘room’ factory. Now if he wants to make a phone call to some outside office, he will contact the front desk to make the connection to that call which means the front desk is acting as an interface.</li></li></ul><li>Motivating Examples - Cont.<br /><ul><li>Frameworks: It is used in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework since the library knows when an object needs to be created, but not what kind of object it should create, this being specific to the application using the library in the framework.</li></li></ul><li>Motivating Examples – Cont.<br />
  6. 6. Forces<br /><ul><li>It is required to have a set of reusable classes which are flexible enough to be extended.
  7. 7. The client does not know the type of object that need to be created in advance and still want to perform operations on them.</li></li></ul><li>Applicability<br /><ul><li>Factory Method is needed when:
  8. 8. A class can’t anticipate the class of objects it must create.
  9. 9. A class wants its subclasses to specify the objects it creates.
  10. 10. Localization of knowledge of helper sub classes which the classes want to delegate.</li></li></ul><li>Basic Structure<br />
  11. 11. Participants<br /><ul><li>Product(IHttpHandler)
  12. 12. Defines the interface of objects the factory method creates.
  13. 13. ConcreteProduct(ASP.SamplePage_aspx)
  14. 14. Implements the Product Interface
  15. 15. Creator(IHttpHandlerFactory)
  16. 16. Declares the factory method and may provide a default implementation for it.
  17. 17. It returns an object of type Product.
  18. 18. ConcreteCreator(PageHandlerFactory)
  19. 19. Overrides the factory method to return an instance of ConcreteProduct.</li></li></ul><li>Collaborators<br /><ul><li>The Creator relies on the subclass’s factory method and return instance of appropriate ConcreteProduct object.
  20. 20. The Creator executes some sequence of operations on the object.</li></li></ul><li>Consequences<br /><ul><li>The code deals with only with the product interface, therefore it can work with any user defined Concrete Product classes (decoupling subclass details from client classes).
  21. 21. New concrete classes can be added without recompiling the existing code.
  22. 22. It may lead to many subclasses if the product objects requires one or more additional objects. (Parallel class hierarchy)</li></li></ul><li>Consequences – Cont.<br />
  23. 23. Implementation<br /><ul><li>Two major varieties
  24. 24. Abstract Creator class with no default implementation
  25. 25. Concrete Creator with default implementation.
  26. 26. Solutions:
  27. 27. Parameterized Methods
  28. 28. Templates</li></li></ul><li>Parameterized Factory Methods<br />class Creator {<br />public:<br /> virtual Product* Create(ProductID id) {<br /> if (id == P1) return new MyProduct;<br /> if (id == P2) return new YourProduct;<br /> // other products ...<br /> return 0;<br /> }<br />};<br />// You can subclass the Creator to handle more IDs<br />Product* MyCreator::Create(ProductID id) {<br /> if (id == P3) return new TheirProduct;<br /> // Handle other IDs<br /> return this-&gt;Creator::Create(id);<br />};<br />
  29. 29. Templatized Factory Methods<br />class Creator {<br />public:<br /> Creator() {<br /> // You cannot call the factory method here (why?)<br /> // Use lazy initialization instead<br /> }<br /> virtual Product* CreateProduct() = 0;<br />};<br />template &lt;class T&gt;<br />class StandardCreator: public Creator {<br />public:<br /> virtual Product* CreateProduct() {<br /> return new T; <br /> }<br />}<br />// In the Client<br />StandardCreator&lt;MyProduct&gt; myCreator;<br />
  30. 30. Known Uses<br /><ul><li>It is a very pervasive pattern.
  31. 31. It is used in several places in the Java API. For example, URLConnection has a method getContent that returns the content as an appropriate object (html, gif etc.)
  32. 32. FCL (.Net Framework Class Library) is no exception to the use of Factory Method Pattern. For example, it is used in Systems.Collections.IEnumerable, System.Net.WebRequest, System.Security.Cryptography</li></li></ul><li>Related Patterns<br /><ul><li>Abstract Factory
  33. 33. Template Methods
  34. 34. Prototypes</li></li></ul><li>Basic Example<br />
  35. 35. References <br /><ul><li>Design Patterns, Elements of Reusable Object-Oriented Software, Erich Gamma, et. al., Addison-Wesley, 1994,ISBN 0-201-63361-2
  36. 36. http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html
  37. 37. http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Virtual_Constructor</li>