Copyright W.E. Howden1 Lecture 16: Factories and Frameworks.

Slides:



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

Copyright W. Howden1 Singleton, Wrapper, and Facade Patterns CSE 111.
Template, Command, Iterator and Composite CSE 111 5/9/20151Copyright W. Howden.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Copyright W. Howden1 Lecture 7: Functional and OO Design Descriptions.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Copyright W. Howden1 Lecture 8: O/O Programming. Copyright W. Howden2 Topics OO Programming Languages Developing programs from Designs –Class and method.
Copyright W. Howden1 Lecture 11: UML Terminology and Additional Models and Notation.
Copyright W. Howden1 Lecture 13: Programming by Contract.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Copyright W. Howden1 Lecture 14: Callbacks, Singletons and Wrappers.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Copyright W. Howden1 Lecture 15: Generalization, Polymorphism and States.
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
Lecture 7: UML Class Diagrams CSE 111 7/15/20151Copyright W. Howden.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
C++ fundamentals.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Object Oriented Software Development
Inheritance using Java
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object Oriented Analysis & Design & UML (Unified Modeling Language)1 Part V: Design The Design Workflow Design Classes Refining Analysis Relationships.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Class Relationships Lecture Oo10 Dependencies. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 5 p.69, Chapt 9 130, Chapt 10.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Designing a Persistence Framework With Patterns
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Object Oriented Software Development
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Advanced Object-oriented Design Patterns Creational Design Patterns.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Overview of C++ Polymorphism
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Chapter 3 Implementing Classes
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
CSC 205 Programming II Lecture 5 AWT - I.
3 Introduction to Classes and Objects.
Factory Patterns 1.
Methods Attributes Method Modifiers ‘static’
Chapter 3: Using Methods, Classes, and Objects
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Interfaces and Inheritance
Introduction to Data Structure
Interfaces.
Object-Oriented PHP (1)
Presentation transcript:

Copyright W.E. Howden1 Lecture 16: Factories and Frameworks

Copyright W.E. Howden2 Factory Pattern One definition: –Involves a class that is used to create instances of one or more other classes. e.g. it will contain methods like: A: getInstanceofA() which will create and return an instance of type A

Copyright W.E. Howden3 Factory Motivation 1 Why not just use the constructor for A? A is typically an interface or abstract class i)Actual concrete class may not be known until runtime, via a run time property, and will be determined by the getInstanceA() method ii)Concrete class known during class reuse iii)Except for the factory, the code does not have to know what the concrete class is

Copyright W.E. Howden4 Creator Pattern (Review) Give class B the responsibility of creating instances of class A if: –B aggregates object instances of A –B contains objects of class A –B records instances of objects of class A –B “closely uses” objects of class A –B has the initialization data that will be passed to an object of class A when it is created

Copyright W.E. Howden5 Factory Motivation 2 Why not just put the getInstanceA() code in the places where the Creator pattern suggests such an object should be created? –This code will have the details of what the concrete class looks like or how you get it. Could have poor cohesion since the code is built around using an object with interface/abstract supertype A, and the details do not belong here

Copyright W.E. Howden6 Factories vs Singleton Singleton pattern: returns the single instance of a class –Is this an example of a factory? Sort of, but not really the class of the returned instance is known in the code requesting it as a concrete class, not an interface or abstract class the class of the returned instance is the same class as the place where the getInstance() method is located, it is not an instance of a different class

Copyright W.E. Howden7 DS Factory Example Involves the use of a proxy class for the DataBase subsystem interface First a review of some Java features

Copyright W.E. Howden8 Java Review – Properties Objects A Property object holds a set of string pairs where the first is a key and the second is a property associated with that key. (Subclass of HashTable class.) A.getProperty(s) will return the string that is associated with the key s in the Properties object A A.setProperty(s, t) will set create a property s with value t in the Property object A

Copyright W.E. Howden9 Java Review – System Class the System class –special class with static methods –e.g. System.out will return the standard output device, as used in System.out.printline(x) to print out the string value of x –System.getProperties() will return a properties object that contains configuration information. –System.getProperty(s) will return a particular system property whose string name is s

Copyright W.E. Howden10 Java Review – Changing System Properties Properties sysProps=System.getProperties(); sysProps.setProperty(xxx,yyy);

Copyright W.E. Howden11 Java Review – Class Object For every class A in an application, the JRE provides an immutable object of type Class with information about A. Suppose that the fully qualified name for A is App.A. Getting the Class object for A. Suppose x is an instance of A. Three alternatives: i)Class class = x.getClass(); ii)Class class = A.class iii)Class class = Class.forName(“App.A”) //will also load class A

Copyright W.E. Howden12 Java Review – Creating An Object from its Class Object Suppose x is an instance of the Class class, containing information about a class A x.newInstance() creates an object of the type Object, having the properties of class A. Suppose IA is an interface for A. (IA) x.newInstance will cast the new object of type Object as an object of type IA.

Copyright W.E. Howden13 DS Factory Example We will assume the use of a DataBase proxy class that interfaces with a vendor supplied data base product Suppose that there are several different choices of data bases, so we want to create an instance of the correct proxy class at run time. The rest of the system just has to know the interface IDataBase for the DataBase proxy object

Copyright W.E. Howden14 DS Object Factory Strategy Assume that the System properties object contains the fully qualified name of the DB Proxy class to be used Have a DBFactory Class with an IDBProxy variable, and a getProxy() method that returns the value of the variable if it is not null, or if it is null creates an instance of the current DB Proxy class Use the Class.forName method to load the desired DBProxy class and create an instance of its class object

Copyright W.E. Howden15 DBProxy Factory Class DBProxyFactory { IDBProxy dbProxy;... IDBProxy getDBProxy() { if dbProxy ==null { String className = System.getProperty(“dbProxy”); dbProxy = (IDBProxy) Class.forName(className).newInstance(); } return dbProxy; }

Copyright W.E. Howden16 Type 1 Factory Problem: Want to have a mechanism for creating an instance of a set of related objects without specifying its concrete classes Solution: Construct a method that returns an object that is only characterized by an interface or parent class, not the actual concrete class that is returned Above DS example is a type 1 factory

Copyright W.E. Howden17 Type 2 Factory Problem: have a method getInstance() in class A that returns an instance of a class B. Want subclasses of A to be able to return different kinds of objects. Solution: Subtype class A with classes that have methods that override getInstance() and return the desired types of objects. The new types should be subclasses of B. In the following the DSFramework is a type 2 factory

Copyright W.E. Howden18 Code Re-use - Frameworks Functional programming re-use: subroutines –re-usable low level layers, high-level structure and flow added by user programmer, used by higher levels O/O programming re-use: classes –re-usable low level layers, high-level structure and flow added by user programmer, used by higher levels O/O re-use: frameworks –re-usable high-level layers containing “major flow”, details are added by user programmer by subclassing framework classes, uses lower levels

Copyright W.E. Howden19 Framework Examples - AWT Java AWT contains classes for frame, button, text boxes etc. To build a GUI you –subclass and define new constructors –implement required event handling routines that are specified in interfaces Does not quite fit the idea of a framework’s containing the major flow, since you also implement the presentation logic

Copyright W.E. Howden20 DS DB Review 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

Copyright W.E. Howden21 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

Copyright W.E. Howden22 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.

Copyright W.E. Howden23 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

Copyright W.E. Howden24 Factory Based Framework Pattern

Copyright W.E. Howden25 Framework Subclasses for DS

Copyright W.E. Howden26 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

Copyright W.E. Howden27 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.

Copyright W.E. Howden28 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

Copyright W.E. Howden29 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();}

Copyright W.E. Howden30 PersistentObject Interface public interface PersistentObject public void read(File file) throws IOException public void write(File file) throws IOException public String key()

Copyright W.E. Howden31 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(File file) throws IOException public void write(File file) throws IOException

Copyright W.E. Howden32 Technical Underpinnings Cannot create an instance of an abstract class or interface Can declare a variable of type interface or abstract class –of course any value of this variable will have to have a concrete type, but it will need to be a subtype of the declared type

Copyright W.E. Howden33 Sample DataBase Method Design – DataBase Constructor

Copyright W.E. Howden34 DS Framework and Type 2 Factories Type 1 contains a create() method that is declared to return instances of an interface or abstract class, but actually returns instances of a concrete class. Why? class is unknown until run time, and the rest of the code does not care Type 2 contains a create() method in a class that is abstract, and will be defined when the class is subclassed. This will coincide with the subclassing of the interface or subclass the abstract create() is declared to return. Why? So we can re-use the code in different applications which will have a different concrete subclass

Copyright W.E. Howden35 DS Framework and Template Classes Template classes – have a parameter for which a class name will be substituted when the template class is used. Preprocessor macro substitution. E.g. class GeneralDataBase { [ ] persistentObjects persistentObject.... // reading in objects from file persistentObject = new ; persistentObject.read(file);... }

Copyright W.E. Howden36 Template Classes and Factories Type 1 –may not know class until run time so is not applicable Type 2 –What if we substitute a class T that does not have the required interface? e.g. it has no read method –Does not allow us to model the idea that all classes that should be used implement an abstract concept that is an important aspect of the domain