46
Copyright W.E. Howden 1 Factories and Frameworks CSE 111 06/22/22

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

Embed Size (px)

Citation preview

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

Copyright W.E. Howden 1

Factories and Frameworks

CSE 111

04/11/23

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

Variations

• Factory method

• Object factory

• Abstract factory

• Factory pattern

Copyright W.E. Howden 204/11/23

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

Elements of a Pattern

• Context

• Problem

• Solution

04/11/23 Copyright W.E. Howden 3

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

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. Howden 404/11/23

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

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.

04/11/23 Copyright W.E. Howden 5

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

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.

04/11/23 Copyright W.E. Howden 6

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

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. Howden 704/11/23

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

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

04/11/23 Copyright W.E. Howden 8

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

Object Factories withPamela, 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

04/11/23 Copyright W.E. Howden 9

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

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

04/11/23 Copyright W.E. Howden 10

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

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

04/11/23 Copyright W.E. Howden 11

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

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.

04/11/23 Copyright W.E. Howden 12

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

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

04/11/23 Copyright W.E. Howden 13

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

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

04/11/23 Copyright W.E. Howden 14

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

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

04/11/23 Copyright W.E. Howden 15

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

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.

04/11/23 Copyright W.E. Howden 16

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

Use of Abstract Factory for New DS Feature

DL.getDateDescription()1. creates an instance x of a date components

subclass of AbstractDateComponentse.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

04/11/23 Copyright W.E. Howden 17

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

AbstractDateComponentsAbstract Factory

04/11/23 Copyright W.E. Howden 18

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

Suggestions Factory

04/11/23 Copyright W.E. Howden 19

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

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()

04/11/23 Copyright W.E. Howden 20

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

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”

04/11/23 Copyright W.E. Howden 21

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

Observations

• Still need to determine some details– how do we know which subclass of the

AbstractDateComponents superclass to use?

04/11/23 Copyright W.E. Howden 22

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

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.

04/11/23 Copyright W.E. Howden 23

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

Factory Pattern Inheritance Hierarchies

04/11/23 Copyright W.E. Howden 24

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

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

04/11/23 Copyright W.E. Howden 25

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

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

04/11/23 Copyright W.E. Howden 26

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

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

04/11/23 Copyright W.E. Howden 27

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

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

04/11/23 Copyright W.E. Howden 28

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

Dependency InversionNormal versus Inverted

04/11/23 Copyright W.E. Howden 29

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

Factory Patterns and Frameworks

• A framework is a factory pattern

• Emphasis on re-use

• Factory Pattern Example for the DS– Simple Data Base Framework

04/11/23 Copyright W.E. Howden 30

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

Copyright W.E. Howden 31

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

04/11/23

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

Copyright W.E. Howden 32

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

04/11/23

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

Copyright W.E. Howden 33

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

04/11/23

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

Copyright W.E. Howden 34

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

04/11/23

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

Copyright W.E. Howden 35

Framework Pattern

+abstract AppClassInterface create()

AbstractApplication

+AppClassInterface create()

ConcreteApplication

«interface»AppClassInterface

AppConcreteClass

+ AppClassInterface create(){ return new AppConcreteClass(); }

Code for methods in AbstractApplication written using abstract create()method as though it returned a concrete object, satisfying theAppClassInterface

04/11/23

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

Copyright W.E. Howden 36

Framework Subclasses for DS

DataBaseFramework «interface»PersistentObject

DataBase MemberData

04/11/23

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

Copyright W.E. Howden 37

DataBase Framework Classclass 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

04/11/23

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

Copyright W.E. Howden 38

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.

04/11/23

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

Copyright W.E. Howden 39

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

04/11/23

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

Copyright W.E. Howden 40

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();}

04/11/23

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

Copyright W.E. Howden 41

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

04/11/23

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

Copyright W.E. Howden 42

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

04/11/23

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

Copyright W.E. Howden 43

PersistentObject Interface

public interface PersistentObject

public void read(BufferedReader inFile) throws IOException

public void write(PrintWriter outFile) throws IOException

public String key()

04/11/23

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

Copyright W.E. Howden 44

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

04/11/23

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

Copyright W.E. Howden 45

Sample DataBase Method Design – DataBase Constructor

04/11/23

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

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

04/11/23 Copyright W.E. Howden 46