1 / 12

Factory Method Pattern

CSci 658 Software Language Engineering Factor Method Design Pattern Spring Semester 2018 Lecture Notes. 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

astern
Download Presentation

Factory Method Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSci 658Software Language EngineeringFactor Method Design PatternSpring Semester 2018Lecture 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, 8 February 2018

  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. Application uses uses creates NameFactory Namer FirstFirst LastFirst A Simple Factory 2

  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.) 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); } } } 5

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

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

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

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

  12. Acknowledgement • Acxiom Corporation supported the creation of this presentation as part of a grant titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).” • The research team consisted of Dr. Conrad Cunningham, PhD students Yi Liu and PallaviTadepalli, and MS students Mingxian Fu and Hui Xiong. 10

More Related