mohammed al-dhelaan csci 253 object oriented design instructor: brad taylor 06/02/2009 factory...

Click here to load reader

Upload: sheena-robinson

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Slide 1

Mohammed Al-DhelaanCSci 253Object Oriented DesignInstructor: Brad Taylor06/02/2009Factory Method Pattern1OverviewIntroductionFactory MethodStructureImplementationExampleConclusionReferences

2IntroductionReusability is a goal for design patternsDesign patterns help programmers identify reoccurring design issuesDesign Patterns also help insure encapsulation and information hiding Factory method pattern is an object oriented design pattern.Its considered a creational pattern

3Factory MethodThe problem:We dont know when to instantiate object!

Factory method defers instantiation to subclassesIts goal is when to instantiate more than what to instantiateUses an interface that decides what subclass to choose, then instantiate an object from the subclass that was chosen

4Factory MethodThe user code doesnt create objectsThe factory method instantiates objectsDetermination of what class to instantiate is handled at runtime5Factory MethodWhen to use the Factory Method pattern:The superclass cannot determine what class to instantiate objects fromThe superclass wants its subclass to specify the objects

Factory Method pattern helps hiding the core classes from clients 6Structure

7ImplementationThe Creator class is designed as an abstract classNo implementation for the abstract classSubclasses must have an implementation that deals with the Factory Method 8Implementation9

Implementationpublic 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

10ImplementationThe Creator class is designed as a concrete classCreate the object then let the subclass overrides itMore flexible Involves the concept of overriding

11ImplementationUsing a parameterized Factory MethodIn this implementation we have to use an identifier From that identifier we can know what object to create

12Implementationpublic 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

13Implementationpublic 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

14Implementationpublic 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

15ImplementationTemplates instead of subclassesC++Naming conventionsGood for readability 16Example17

Examplepublic 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 class18Examplepublic 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 class19Examplepublic 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 class20ConclusionIntroductionFactory MethodStructureImplementationExampleConclusionReferences

21ReferencesGamma, 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.htmlAllAppLaps.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