Presentation is loading. Please wait.

Presentation is loading. Please wait.

Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.

Similar presentations


Presentation on theme: "Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes."— Presentation transcript:

1 Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes

2 Factory Method Pattern This is a set of slides to accompany chapter 8 of Mark Grand’s book Patterns in Java : a catalog of reusable design patterns illustrated with UML (John Wiley & Sons, 1998) Created: 19 August 2004 Revised: 20 April 2010

3 Context A reusable application that must work with objects from different subclasses of a base class Wish to keep application independent of the actual subclass being used  Invoke only operations on the base class  Delegate creation of the actual subclass object to a special class – factory 1

4 A Simple Factory 2 Application NameFactoryNamer FirstFirstLastFirst uses creates

5 A Simple Factory (cont.) public class Namer { public String getFirst () {return first; } public String getLast () {return last; } protected void setFirst ( String f) {first = f; } protected void setLast (String l) {last = l; } private String first; private String last; } 3

6 A Simple Factory (cont.) public class FirstFirst extends Namer { public FirstFirst (String s) { int i = s.lastIndexOf (" "); if (i > 0) {setFirst (s.substring(0, i).trim() ); setLast (s.substring(i+1).trim() ); } else {setFirst (" "); setLast(s); } 4

7 A Simple Factory (cont.) 5 public class LastFirst extends Namer { public LastFirst (String s) { int i = s. indexOf (","); if (i > 0) {setFirst (s.substring(i+1).trim() ); setLast (s.substring(0,i).trim() ); } else {setFirst (" "); setLast(s); }

8 A Simple Factory (cont.) 6 public class NameFactory { public Namer gerNamer (String s) { int i = s. indexOf (","); if (i > 0) return new LastFirst(s); else return new FirstFirst(s); }

9 A Simple Factory (cont.) Application … NameFactory nf = new NameFactory (); … Namer n = nf.getNamer(s); … String l = n.getLast(); 7

10 Solution 8 Product operation1 opertation2 … CreationRequestor newDocument … > FactoryIF createProduct (discriminator):Product ConcreteProduct operation1 opertation2 … Factory createProduct (discriminator):Product 1 * 1 * Creates Uses Requests_creation * 1 creator requestor abstract superclass Concrete class instantiated by objects Application independent class Application independent interface Application-specific class. Implement factory interface. Has method to create ConcreateProduct objects

11 Consequences Creation-requestor class independent from concrete product class Set of product classes may change   only affects factory class 9

12 Acknowledgement 10 This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).” This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).”


Download ppt "Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes."

Similar presentations


Ads by Google