Factory Method Design Pattern in Software Engineering
120 likes | 155 Vues
Learn about the Factory Method design pattern to create reusable applications working with objects from different subclasses independently. These lecture notes accompany Chapter 8 of Mark Grand’s book "Patterns in Java" (1998). Understand the application context, implementation with examples using Java, and the consequences and acknowledgments related to this pattern in software development.
Factory Method Design Pattern in Software Engineering
E N D
Presentation Transcript
CSci 658Software Language EngineeringFactor Method Design PatternSpring Semester 2018Lecture 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 (John Wiley & Sons, 1998) Created: 19 August 2004 Revised: 20 April 2010, 8 February 2018
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
Application uses uses creates NameFactory Namer FirstFirst LastFirst A Simple Factory 2
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
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
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
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
A Simple Factory (cont.) • Application … NameFactory nf = new NameFactory (); … Namer n = nf.getNamer(s); … String l = n.getLast(); 7
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
Consequences • Creation-requestor class independent from concrete product class • Set of product classes may change • only affects factory class 9
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