Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright W.E. Howden1 Factories and Frameworks CSE 111 1/15/2015.

Similar presentations

Presentation on theme: "Copyright W.E. Howden1 Factories and Frameworks CSE 111 1/15/2015."— Presentation transcript:

1 Copyright W.E. Howden1 Factories and Frameworks CSE 111 1/15/2015

2 Variations Factory method Object factory Abstract factory Factory pattern Copyright W.E. Howden21/15/2015

3 Elements of a Pattern Context Problem Solution 1/15/2015Copyright W.E. Howden3

4 Factory Method A method, e.g. that you call create(), that you write with an instance of an object Why not just use a class constructor? Because you want to program at an abstract level, using an abstract class or interface. You will later subclass to fill in details. Abstract classes and interfaces do not have constructors, so you simulate construction with a method that returns an object of the abstract type Copyright W.E. Howden41/15/2015

5 Terminology and create() method 1 In UML collaboration diagrams, you use a create() message from object x to object y, to indicate that x calls the constructor for y to create it. When we implement the diagram, there is no create() method, x simply calls the constructor for x. 1/15/2015Copyright W.E. Howden5

6 Terminology and create() method 2 Factory methods, which we may choose to call “create()”, are not exactly the same thing as the create() in a UML CD. But there is a connection: a create() factory method simulates the creation of an object at an abstract level, (e.g. return type is a Java interface). When we will implement it with a concrete method we use the constructor for the class that implements the interface. 1/15/2015Copyright W.E. Howden6

7 Object Factory A class with a method(s) whose purpose is to return an instance of one or more classes May have logic to determine which type Gathers the logic in one place instead of distributing around to other classes Facilitates corrections and changes because all the logic is one place Copyright W.E. Howden71/15/2015

8 Object Factory Pattern Context –Manufacturing objects Problem –If a new kind comes along, or we want to change how it is built, we have to change our code Solution –Isolate the manufacturing in an object factory. You tell it you want an item and it returns it 1/15/2015Copyright W.E. Howden8

9 Object Factories with Pamela, Fred, Sally, Jane, Henry 1.Pamela wants to manufacture a souvenir 2.Fred can produce them 3.Pamela hires Sally to run the facility and tells her about Fred 4.Jane places an order with Sally 5.Sally asks Fred to construct a souvenir 6.Fred returns it to Sally 7.Sally takes care of packaging and billing and ships the object to Jane 1/15/2015Copyright W.E. Howden9

10 Object Factory Analysis Fred is an object factory Sally is a manufacturing facility Fred could be passed in with the Sally constructor, so she knows him Under certain conditions, we may want to replace Fred with someone else who, for example produces better quality items 1/15/2015Copyright W.E. Howden10

11 Abstract Object Factory Pattern Context A set of factory methods for creating parts Problem May want to change the methods Solution Use an abstract class with abstract definitions for the factory methods. Subclass with method definitions 1/15/2015Copyright W.E. Howden11

12 AOF and Abstract Parts Object Context –manufacturing objects made from a variety of parts Problem –Suppose we want to be able to change properties of the parts, but they are still the same type Solution –Isolate the manufacturing in an object factory. When you ask for an item you supply an instance of a subclass of an abstract parts supplier. Replace it with a new subclass, when necessary. 1/15/2015Copyright W.E. Howden12

13 Abstract Factories with Pamela, Fred, Jane, Sally and Henry 1.Pamela wants to manufacture a souvenir 2.Fred can produce them 3.Pamela hires Sally to run the facility and tells her about Fred 4.Jane places an order with Sally 5.Sally gets a supplier, say Henry, for the parts for the souvenir who conforms to the PartsSuppliersAssociation standards 6.Sally asks Fred to construct an object using Henry 7.Fred was trained to use suppliers who conform to the PartsSuppliersAssociation standards, who all have the same API 8.Fred uses Henry for parts to construct the object and returns it to Sally 9.Sally takes care of packaging and billing and ships the object to Jane 1/15/2015Copyright W.E. Howden13

14 Analysis Fred is an object Factory, who returns a souvenir object Henry is an (instance of a) subclass of the PartsSuppliersAssociation class, an abstract factory. He has a definition for the abstract methods in PartsSuppliersAssociation Henry can be replaced with another supplier if we want to change the parts used, e.g. cheaper ones Fred is designed to work with the PSA superclass for Henry so can also work with any other subclass object. Sally is still the production facility 1/15/2015Copyright W.E. Howden14

15 DS Example Terminology –a dater wants to go out on a date –the DS returns a datee (assuming the request is successful) –The dater and the datee to out on a date –There are different kinds of dates e.g. professional sports event, dining, activity of some kind 1/15/2015Copyright W.E. Howden15

16 DS Example Suppose that after the dater/user gets a datee, the dater can ask for suggestions System returns three alternatives: a prof. sports, a dining, and an activity date Suggestions are constructed using a date components object. We expect to use different components for different cities. 1/15/2015Copyright W.E. Howden16

17 Use of Abstract Factory for New DS Feature DL.getDateDescription() 1.creates an instance x of a date components subclass of AbstractDateComponents e.g. SDDateComponents subclass 2.creates an instance y of DateSuggestions, passing x as a parameter to the constructor 3.calls y.getSuggestions to get a date suggestion, which is returned as a string, with three parts to it for the different kinds of dates 1/15/2015Copyright W.E. Howden17

18 AbstractDateComponents Abstract Factory 1/15/2015Copyright W.E. Howden18

19 Suggestions Factory 1/15/2015Copyright W.E. Howden19

20 Details 1 The methods in the concrete subclasses of AbstractDateComponents return the contents of the associated attribute variables The getSuggestions method in the DateSuggestions object calls components.getSportSuggestion(), components.getDinnerSuggstion(), and components.getActivitySuggestion() 1/15/2015Copyright W.E. Howden20

21 Details 2 The getSuggestions method concatenates the result, with an “or” separator For the given subclass of the AbstractDatecomponets, we will get back “Aztecs Hockey Game or The Beach House in Cardiff or Horseback Riding in Julian” 1/15/2015Copyright W.E. Howden21

22 Observations Still need to determine some details –how do we know which subclass of the AbstractDateComponents superclass to use? 1/15/2015Copyright W.E. Howden22

23 Factory Pattern Context –Application or program whose logic is reoccurring Problem –It is not a reusable component with an interface. It is more of a framework with variations in the parts. Solution –Construct a program that uses abstract factory methods to create abstract objects. Re-use the program by subclassing the abstract objects with concrete objects, and by defining the factory methods to call their constructors. 1/15/2015Copyright W.E. Howden23

24 Factory Pattern Inheritance Hierarchies 1/15/2015Copyright W.E. Howden24

25 Pamela’s Factory Empire Pamela wants to set up a chain of regional souvenir manufacturing facilities 1.A turnkey factory is designed that can be shipped out to a new location 2.Each factory is standard, except there is a changeable position for a souvenir builder who can build souvenirs for the local location 3.The souvenir builders all have to match an “abstract builder”, with the same set of standard capabilities, called AbstractFred. This makes it possible to build a turnkey operation 4.Each new factory is shipped out with a standard copy of Sally, the manager 1/15/2015Copyright W.E. Howden25

26 Factory Pattern with Pamela, Fred, Jane, Sally and Henry 1.Pamela ships a factory out to city x, along with a copy of Sally 2.A LocalFred is found who implements AbstractFred 3.The factory is setup, substituting LocalFred for AbstractFred 4.Jane orders a Souvenir from Sally 5.At the right point in the production Fred gets an order, he builds a souvenir and returns it to the ordering entity 6.Sally confirms the factory has billed, packaged and then mailed the package with the souvenir to Jane 1/15/2015Copyright W.E. Howden26

27 Analysis The turnkey factory is an abstract program with an abstract create() method that returns objects of type AbstractFred When the factory is located at x, AbstractFred is subclassed to get LocalFred, and the turnkey factory is subclasssed to get a concrete factory in which factory method create() returns an instance of local Fred 1/15/2015Copyright W.E. Howden27

28 Factory Pattern Dependency Inversion Dependency inversion in frameworks Normal: principal class depends on and re-uses subordinate classes data and logic Inverted: subordinate (sub)classes depend on and reuse principal (super)class data and logic 1/15/2015Copyright W.E. Howden28

29 Dependency Inversion Normal versus Inverted 1/15/2015Copyright W.E. Howden29

30 Factory Patterns and Frameworks A framework is a factory pattern Emphasis on re-use Factory Pattern Example for the DS –Simple Data Base Framework 1/15/2015Copyright W.E. Howden30

31 Copyright W.E. Howden31 My DS DB Design DB constructor is passed a file name File is assumed to be in a predetermined format, and to contain MemberData records DB reads in the records and stores them in a MemberData vector All data base operations (isMember(name), getMemberData(name), getNext(), etc.) are performed on this vector When system terminates, the vector is written back out to the file 1/15/2015

32 Copyright W.E. Howden32 DB ReUse Opportunity Basic idea is quite general –read in a set of records and store them in a vector. Write back out on termination. –access records sequentially or using a key –the only problem specific details are the contents of the record 1/15/2015

33 Copyright W.E. Howden33 DB Framework Strategy – Hiding the Details Reading and writing records from the file into the DB vector will require knowledge of the details of the record –use expert pattern/object animation and tell the records to read and write themselves to the file. They know what they look like. –implies the records will have a read and write method 1/15/2015

34 Copyright W.E. Howden34 DB Framework Strategy – Overall Structure DataBaseFramework is a data base subsystem, complete except for some details Framework abstractions –DataBaseFramework, an abstract class, containing: createPersistentObject(), an abstract method –PersistentObject, an Interface, used to specify the type of object returned by createPersistentObject() User will subclass DataBaseFramework and PersistentObject 1/15/2015

35 Copyright W.E. Howden35 Framework Pattern 1/15/2015

36 Copyright W.E. Howden36 Framework Subclasses for DS 1/15/2015

37 Copyright W.E. Howden37 DataBase Framework Class class DataBaseFramework PersistentObject [ ] persistentObjects int numberObjects int maxObjects = 1500 int accessCounter File objectDataFile public DataBaseFramework(File file) // reads in file and stores records in PersistentObjects[] public void closeDB() // writes objects in PersistentObjects[] back out to file public PersistentObject getFirstObject() public PersistentObject getNextObject() public boolean update(PersistentObject pObj) public boolean belongs(String key) public persistentObject getObject(String key) public boolean add(PersistentObject pObj) public boolean delete(String name) public abstract PersistentObject createPersistentObject() // to be defined 1/15/2015

38 Copyright W.E. Howden38 DataBaseFramework Notes 1 The constructor in the framework has the logic to read in the instances of PersistentObject from the file and store them in a vector. It uses create() to make the objects and expects the objects to know how to read themselves with their read() method PersistentObject is an interface, and in the framework, create() is abstract. The framework will be subclassed and a definition for create() will be given that constructs instances of a concrete class that implements PersistentObject. 1/15/2015

39 Copyright W.E. Howden39 DataBaseFramework Notes 2 In the subclass for the framework, the database methods that return records from the database will be refined so that the object that is returned is cast to the concrete implementation of PersistentObject 1/15/2015

40 Copyright W.E. Howden40 DataBase Implementation of DataBase Framework public class DataBase extends DataBaseFramework public DataBase(File file){super(file)} public PersistentObject createPersistentObject() {return new MemberData();} public MemberData getMemberData(String name) { return (MemberData) getObject(name); } public MemberData getFirst() { return (MemberData) getFirstObject();} public MemberData getNext() { return (MemberData) getNextObject();} 1/15/2015

41 Copyright W.E. Howden41 Notes on PO Read and Write Methods A PO’s read() method will read in data from the file, and cause the PO’s attributes to be set to the data in the file. The object is reading itself in. A PO’s write() method will write itself, i.e. its attributes, out to the named file 1/15/2015

42 Copyright W.E. Howden42 Consistency of PO read() and write() with DB Framework DB Framework will create reader and writer streams for the file used for the DB data –e.g. BufferedReader and PrintWriter The file argument passed to the read() and write() methods will be a stream that was created by the framework from/to which the PO will read/write. The PO and the Framework will have to be consistent on the kind of argument used here. –e.g. Framework creates a BufferedReader for reading from the file, which it passes in the read() method 1/15/2015

43 Copyright W.E. Howden43 PersistentObject Interface public interface PersistentObject public void read(BufferedReader inFile) throws IOException public void write(PrintWriter outFile) throws IOException public String key() 1/15/2015

44 Copyright W.E. Howden44 MemberData Implementation of PersistentObject public class MemberData implements PersistentObject public String name public DateeData dateeData public AdminData adminData public MemberData() public String key() {return name;) public void read(BufferedReader inFile) throws IOException public void write(PrintWriter outFile) throws IOException 1/15/2015

45 Copyright W.E. Howden45 Sample DataBase Method Design – DataBase Constructor 1/15/2015

46 Assignment Use the factory pattern to design a data base subsystem for your project This will result in the development of a framework that could be used for similar data bases in other applications Your project may require different uses of the ideas, if it differs from my DS example 1/15/2015Copyright W.E. Howden46

Download ppt "Copyright W.E. Howden1 Factories and Frameworks CSE 111 1/15/2015."

Similar presentations

Ads by Google