Скачать презентацию Factories and Frameworks CSE 111 3 19 2018 Copyright W Скачать презентацию Factories and Frameworks CSE 111 3 19 2018 Copyright W

8d409e9d2a9be1cb2cf7d6c05ff3238c.ppt

  • Количество слайдов: 46

Factories and Frameworks CSE 111 3/19/2018 Copyright W. E. Howden 1 Factories and Frameworks CSE 111 3/19/2018 Copyright W. E. Howden 1

Variations • • Factory method Object factory Abstract factory Factory pattern 3/19/2018 Copyright W. Variations • • Factory method Object factory Abstract factory Factory pattern 3/19/2018 Copyright W. E. Howden 2

Elements of a Pattern • Context • Problem • Solution 3/19/2018 Copyright W. E. Elements of a Pattern • Context • Problem • Solution 3/19/2018 Copyright W. E. Howden 3

Factory Method • A method, e. g. that you call create(), that you write 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 3/19/2018 Copyright W. E. Howden 4

Terminology and create() method 1 • In UML collaboration diagrams, you use a create() 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. 3/19/2018 Copyright W. E. Howden 5

Terminology and create() method 2 • Factory methods, which we may choose to call 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. 3/19/2018 Copyright W. E. Howden 6

Object Factory • A class with a method(s) whose purpose is to return an 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 3/19/2018 Copyright W. E. Howden 7

Object Factory Pattern • Context – Manufacturing objects • Problem – If a new 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 3/19/2018 Copyright W. E. Howden 8

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

Object Factory Analysis • Fred is an object factory • Sally is a manufacturing 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 3/19/2018 Copyright W. E. Howden 10

Abstract Object Factory Pattern • Context • A set of factory methods for creating 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 3/19/2018 Copyright W. E. Howden 11

AOF and Abstract Parts Object • Context – manufacturing objects made from a variety 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. 3/19/2018 Copyright W. E. Howden 12

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

Analysis • Fred is an object Factory, who returns a souvenir object • Henry Analysis • Fred is an object Factory, who returns a souvenir object • Henry is an (instance of a) subclass of the Parts. Suppliers. Association class, an abstract factory. He has a definition for the abstract methods in Parts. Suppliers. Association • 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 3/19/2018 Copyright W. E. Howden 14

DS Example • Terminology – a dater wants to go out on a date 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 3/19/2018 Copyright W. E. Howden 15

DS Example • Suppose that after the dater/user gets a datee, the dater can 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. 3/19/2018 Copyright W. E. Howden 16

Use of Abstract Factory for New DS Feature DL. get. Date. Description() 1. creates Use of Abstract Factory for New DS Feature DL. get. Date. Description() 1. creates an instance x of a date components subclass of Abstract. Date. Components e. g. SDDate. Components subclass 2. creates an instance y of Date. Suggestions, passing x as a parameter to the constructor 3. calls y. get. Suggestions to get a date suggestion, which is returned as a string, with three parts to it for the different kinds of dates 3/19/2018 Copyright W. E. Howden 17

Abstract. Date. Components Abstract Factory 3/19/2018 Copyright W. E. Howden 18 Abstract. Date. Components Abstract Factory 3/19/2018 Copyright W. E. Howden 18

Suggestions Factory 3/19/2018 Copyright W. E. Howden 19 Suggestions Factory 3/19/2018 Copyright W. E. Howden 19

Details 1 • The methods in the concrete subclasses of Abstract. Date. Components return Details 1 • The methods in the concrete subclasses of Abstract. Date. Components return the contents of the associated attribute variables • The get. Suggestions method in the Date. Suggestions object calls components. get. Sport. Suggestion(), components. get. Dinner. Suggstion(), and components. get. Activity. Suggestion() 3/19/2018 Copyright W. E. Howden 20

Details 2 • The get. Suggestions method concatenates the result, with an “or” separator Details 2 • The get. Suggestions method concatenates the result, with an “or” separator • For the given subclass of the Abstract. Datecomponets, we will get back “Aztecs Hockey Game or The Beach House in Cardiff or Horseback Riding in Julian” 3/19/2018 Copyright W. E. Howden 21

Observations • Still need to determine some details – how do we know which Observations • Still need to determine some details – how do we know which subclass of the Abstract. Date. Components superclass to use? 3/19/2018 Copyright W. E. Howden 22

Factory Pattern • Context – Application or program whose logic is reoccurring • Problem 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. 3/19/2018 Copyright W. E. Howden 23

Factory Pattern Inheritance Hierarchies 3/19/2018 Copyright W. E. Howden 24 Factory Pattern Inheritance Hierarchies 3/19/2018 Copyright W. E. Howden 24

Pamela’s Factory Empire • Pamela wants to set up a chain of regional souvenir 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 Abstract. Fred. This makes it possible to build a turnkey operation 4. Each new factory is shipped out with a standard copy of Sally, the manager 3/19/2018 Copyright W. E. Howden 25

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

Analysis • The turnkey factory is an abstract program with an abstract create() method Analysis • The turnkey factory is an abstract program with an abstract create() method that returns objects of type Abstract. Fred • When the factory is located at x, Abstract. Fred is subclassed to get Local. Fred, and the turnkey factory is subclasssed to get a concrete factory in which factory method create() returns an instance of local Fred 3/19/2018 Copyright W. E. Howden 27

Factory Pattern Dependency Inversion • Dependency inversion in frameworks • Normal: principal class depends 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 3/19/2018 Copyright W. E. Howden 28

Dependency Inversion Normal versus Inverted 3/19/2018 Copyright W. E. Howden 29 Dependency Inversion Normal versus Inverted 3/19/2018 Copyright W. E. Howden 29

Factory Patterns and Frameworks • A framework is a factory pattern • Emphasis on Factory Patterns and Frameworks • A framework is a factory pattern • Emphasis on re-use • Factory Pattern Example for the DS – Simple Data Base Framework 3/19/2018 Copyright W. E. Howden 30

My DS DB Design • DB constructor is passed a file name • File My DS DB Design • DB constructor is passed a file name • File is assumed to be in a predetermined format, and to contain Member. Data records • DB reads in the records and stores them in a Member. Data vector • All data base operations (is. Member(name), get. Member. Data(name), get. Next(), etc. ) are performed on this vector • When system terminates, the vector is written back out to the file 3/19/2018 Copyright W. E. Howden 31

DB Re. Use Opportunity • Basic idea is quite general – read in a DB Re. Use 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 3/19/2018 Copyright W. E. Howden 32

DB Framework Strategy – Hiding the Details • Reading and writing records from the 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 3/19/2018 Copyright W. E. Howden 33

DB Framework Strategy – Overall Structure • Data. Base. Framework is a data base DB Framework Strategy – Overall Structure • Data. Base. Framework is a data base subsystem, complete except for some details • Framework abstractions – Data. Base. Framework, an abstract class, containing: • create. Persistent. Object(), an abstract method – Persistent. Object, an Interface, used to specify the type of object returned by create. Persistent. Object() • User will subclass Data. Base. Framework and Persistent. Object 3/19/2018 Copyright W. E. Howden 34

Framework Pattern 3/19/2018 Copyright W. E. Howden 35 Framework Pattern 3/19/2018 Copyright W. E. Howden 35

Framework Subclasses for DS 3/19/2018 Copyright W. E. Howden 36 Framework Subclasses for DS 3/19/2018 Copyright W. E. Howden 36

Data. Base Framework Class class Data. Base. Framework Persistent. Object [ ] persistent. Objects Data. Base Framework Class class Data. Base. Framework Persistent. Object [ ] persistent. Objects int number. Objects int max. Objects = 1500 int access. Counter File object. Data. File public Data. Base. Framework(File file) // reads in file and stores records in Persistent. Objects[] public void close. DB() // writes objects in Persistent. Objects[] back out to file public Persistent. Object get. First. Object() public Persistent. Object get. Next. Object() public boolean update(Persistent. Object p. Obj) public boolean belongs(String key) public persistent. Object get. Object(String key) public boolean add(Persistent. Object p. Obj) public boolean delete(String name) public abstract Persistent. Object create. Persistent. Object() // to be defined 3/19/2018 Copyright W. E. Howden 37

Data. Base. Framework Notes 1 • The constructor in the framework has the logic Data. Base. Framework Notes 1 • The constructor in the framework has the logic to read in the instances of Persistent. Object 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 • Persistent. Object 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 Persistent. Object. 3/19/2018 Copyright W. E. Howden 38

Data. Base. Framework Notes 2 • In the subclass for the framework, the database Data. Base. Framework 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 Persistent. Object 3/19/2018 Copyright W. E. Howden 39

Data. Base Implementation of Data. Base Framework public class Data. Base extends Data. Base. Data. Base Implementation of Data. Base Framework public class Data. Base extends Data. Base. Framework public Data. Base(File file){super(file)} public Persistent. Object create. Persistent. Object() {return new Member. Data(); } public Member. Data get. Member. Data(String name) { return (Member. Data) get. Object(name); } public Member. Data get. First() { return (Member. Data) get. First. Object(); } public Member. Data get. Next() { return (Member. Data) get. Next. Object(); } 3/19/2018 Copyright W. E. Howden 40

Notes on PO Read and Write Methods • A PO’s read() method will read 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 3/19/2018 Copyright W. E. Howden 41

Consistency of PO read() and write() with DB Framework • DB Framework will create 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. Buffered. Reader and Print. Writer • 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 Buffered. Reader for reading from the file, which it passes in the read() method 3/19/2018 Copyright W. E. Howden 42

Persistent. Object Interface public interface Persistent. Object public void read(Buffered. Reader in. File) throws Persistent. Object Interface public interface Persistent. Object public void read(Buffered. Reader in. File) throws IOException public void write(Print. Writer out. File) throws IOException public String key() 3/19/2018 Copyright W. E. Howden 43

Member. Data Implementation of Persistent. Object public class Member. Data implements Persistent. Object public Member. Data Implementation of Persistent. Object public class Member. Data implements Persistent. Object public String name public Datee. Data datee. Data public Admin. Data admin. Data public Member. Data() public String key() {return name; ) public void read(Buffered. Reader in. File) throws IOException public void write(Print. Writer out. File) throws IOException 3/19/2018 Copyright W. E. Howden 44

Sample Data. Base Method Design – Data. Base Constructor 3/19/2018 Copyright W. E. Howden Sample Data. Base Method Design – Data. Base Constructor 3/19/2018 Copyright W. E. Howden 45

Assignment • Use the factory pattern to design a data base subsystem for your 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 3/19/2018 Copyright W. E. Howden 46