250 likes | 453 Vues
The Factory Method Pattern is a creational design pattern in object-oriented programming that defers the instantiation of objects to subclasses, allowing for more flexibility. It addresses situations where a superclass cannot determine the specific class to instantiate. By using an interface, it enables subclasses to specify which objects to create, promoting reusability, encapsulation, and information hiding. This overview covers the pattern’s structure, implementation, and provides practical examples to illustrate its use in software design.
E N D
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 • 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
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
Factory Method • The user code doesn’t create objects • The factory method instantiates objects • Determination of what class to instantiate is handled at runtime
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
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
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
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
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
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
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
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
Implementation • Templates instead of subclasses • C++ • Naming conventions • Good for readability
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
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
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
Conclusion • Introduction • Factory Method • Structure • Implementation • Example • Conclusion • References
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