Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern."— Presentation transcript:

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

2 Overview Introduction Factory Method Structure Implementation Example Conclusion References 2

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 3

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 4

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 5

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 6

7 Structure 7

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 8

9 Implementation 9

10 public abstract class Profile { public Profile (String sName, String sEmail) { m_sName = sName; m_sEmail = sEmail; } public String getName () { return m_sName; } public String getEmail () { return m_sEmail; } public boolean IsEmployee () { return m_bIsEmployee; } public abstract Resource getResource (); protected String m_sName, m_sEmail; protected boolean m_bIsEmployee; } Reference  Goplan Suresh Raj 10

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 11

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 12

13 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

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 14

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 15

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

17 Example 17

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 18

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 19

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 20

21 Conclusion Introduction Factory Method Structure Implementation Example Conclusion References 21

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. Gamma, ErichHelm, RichardDesign Patterns: Elements of Reusable Object-Oriented SoftwareISBN 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 22


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

Similar presentations


Ads by Google