Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern
Overview Introduction Factory Method Structure Implementation Example Conclusion References 2
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 3
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 4
Factory Method The user code doesn’t create objects The factory method instantiates objects Determination of what class to instantiate is handled at runtime 5
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 6
Structure 7
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 8
Implementation 9
public abstract class Profile { public Profile (String sName, String s ) { m_sName = sName; m_s = s ; } public String getName () { return m_sName; } public String get () { return m_s ; } public boolean IsEmployee () { return m_bIsEmployee; } public abstract Resource getResource (); protected String m_sName, m_s ; protected boolean m_bIsEmployee; } Reference Goplan Suresh Raj 10
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 11
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 12
Implementation public class ResourceCreator { public static final int CONFIDENTIAL = 0; public static final int PUBLIC = 1; public Resource createResource (int nID) { switch (nID) { case CONFIDENTIAL: return new ConfidentialResource (); case PUBLIC: return new PublicResource (); } return null; } Reference Goplan Suresh Raj 13
Implementation public class Employee extends Profile { public Employee (String sName, String s ) { super (sName, s ); m_bIsEmployee = true; } public Resource getResource () { ResourceCreator creator = new ResourceCreator (); return creator.createResource (ResourceCreator.CONFIDENTIAL); } Reference Goplan Suresh Raj 14
Implementation public class NonEmployee extends Profile { public NonEmployee (String sName, String s ) { super (sName, s ); m_bIsEmployee = false; } public Resource getResource () { ResourceCreator creator = new ResourceCreator (); return creator.createResource (ResourceCreator.PUBLIC); } Reference Goplan Suresh Raj 15
Implementation Templates instead of subclasses C++ Naming conventions Good for readability 16
Example 17
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 18
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 19
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 20
Conclusion Introduction Factory Method Structure Implementation Example Conclusion References 21
References Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN Gamma, ErichHelm, RichardDesign Patterns: Elements of Reusable Object-Oriented SoftwareISBN Raj, Goplan Suresh. AllAppLaps.com. Tarr, Bob. Factory Patterns. 22