1 / 22

Factory Method Pattern

Factory Method Pattern. Mohammed Al- Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009. Overview. Introduction Factory Method Structure Implementation Example Conclusion References. Introduction. Reusability is a goal for design patterns

pilar
Download Presentation

Factory Method Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Factory Method Pattern Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009

  2. Overview • Introduction • Factory Method • Structure • Implementation • Example • Conclusion • References

  3. Introduction • Reusability is a goal for design patterns • Design patterns help programmers identify reoccurring design issues • Design Patterns also help insure encapsulation and information hiding • Factory method pattern is an object oriented design pattern. • It’s considered a creational pattern

  4. Factory Method • The problem: • We don’t know when to instantiate object! • Factory method defers instantiation to subclasses • It’s goal is when to instantiate more than what to instantiate • Uses an interface that decides what subclass to choose, then instantiate an object from the subclass that was chosen

  5. Factory Method • The user code doesn’t create objects • The factory method instantiates objects • Determination of what class to instantiate is handled at runtime

  6. Factory Method • When to use the Factory Method pattern: • The superclass cannot determine what class to instantiate objects from • The superclass wants its subclass to specify the objects • Factory Method pattern helps hiding the core classes from clients

  7. Structure

  8. Implementation • The Creator class is designed as an abstract class • No implementation for the abstract class • Subclasses must have an implementation that deals with the Factory Method

  9. Implementation

  10. Implementation • publicabstractclass Profile { • public Profile (String sName, String sEmail){ • m_sName = sName; • m_sEmail = sEmail; • } • public String getName() {returnm_sName;} • public String getEmail() {returnm_sEmail;} • publicbooleanIsEmployee() {returnm_bIsEmployee;} • publicabstract Resource getResource(); • protected String m_sName, • m_sEmail; • protectedbooleanm_bIsEmployee; • } • Reference  Goplan Suresh Raj

  11. Implementation • The Creator class is designed as a concrete class • Create the object then let the subclass overrides it • More flexible • Involves the concept of overriding

  12. Implementation • Using a parameterized Factory Method • In this implementation we have to use an identifier • From that identifier we can know what object to create

  13. Implementation • public class ResourceCreator { • public static final int CONFIDENTIAL = 0; • public static final int PUBLIC = 1; • public Resource createResource (intnID) { • switch (nID) { • case CONFIDENTIAL: • return new ConfidentialResource (); • case PUBLIC: • return new PublicResource (); • } • return null; • } • } • Reference  Goplan Suresh Raj

  14. Implementation • public class Employee extends Profile { • public Employee (String sName, String sEmail) { • super (sName, sEmail); • m_bIsEmployee = true; • } • public Resource getResource () { • ResourceCreator creator = new ResourceCreator (); • return creator.createResource (ResourceCreator.CONFIDENTIAL); • } • } • Reference  Goplan Suresh Raj

  15. Implementation • public class NonEmployee extends Profile { • public NonEmployee (String sName, String sEmail) { • super (sName, sEmail); • m_bIsEmployee = false; • } • public Resource getResource () { • ResourceCreator creator = new ResourceCreator (); • return creator.createResource (ResourceCreator.PUBLIC); • } • } • Reference  Goplan Suresh Raj

  16. Implementation • Templates instead of subclasses • C++ • Naming conventions • Good for readability

  17. Example

  18. Example • public class Person { • // name string • public String name; • // gender : M or F • private String gender; • public String getName() { • return name; • } • public String getGender() { • return gender; • } • }// End of class

  19. Example • public class Male extends Person { • public Male(String fullName) { • System.out.println("Hello Mr. "+fullName); • } • }// End of class • public class Female extends Person { • public Female(String fullNname) { • System.out.println("Hello Ms. "+fullNname); • } • }// End of class

  20. Example • public class SalutationFactory { • public static void main(String args[]) { • SalutationFactory factory = new SalutationFactory(); • factory.getPerson(args[0], args[1]); • } • public Person getPerson(String name, String gender) { • if (gender.equals("M")) • return new Male(name); • else if(gender.equals("F")) • return new Female(name); • else • return null; • } • }// End of class

  21. Conclusion • Introduction • Factory Method • Structure • Implementation • Example • Conclusion • References

  22. References • Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN 0-201-63361-2.  • Raj, Goplan Suresh. http://gsraj.tripod.com/design/creational/factory/factory.html • AllAppLaps.com. http://www.allapplabs.com/java_design_patterns/factory_pattern.htm • Tarr, Bob. Factory Patterns. http://userpages.umbc.edu/~tarr/dp/lectures/Factory.pdf

More Related