Partitioning Patterns How to partition complex actors and concepts into multiple classes. Layered Initialization Filter Composite.

Slides:



Advertisements
Similar presentations
Testing Relational Database
Advertisements

Systems Architecture Use Case Diagram, System Overview, Class Diagram Design Patterns (weve used) Refactorings (weve used) Table of Contents.
Lecture 15: I/O and Parsing
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Design Patterns CMPS Design Patterns Consider previous solutions to problems similar to any new problem ▫ must have some characteristics in common.
Chapter 10 Introduction to Arrays
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
March 2004Object Oriented Design1 Object-Oriented Design.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 2 Data Design and Implementation. Homework You should have already read section 2.2 You should have already read section 2.2 Read Section 2.3.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Chapter 10 Classes Continued
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CVSQL 2 The Design. System Overview System Components CVSQL Server –Three network interfaces –Modular data source provider framework –Decoupled SQL parsing.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Java Software Solutions Foundations of Program Design Sixth Edition
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Programming in Java CSCI-2220 Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Java Design Patterns SD.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Design Patterns Definition:
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Memento Design Pattern
I/O Basics.
Java Programming Language
An Introduction to Software Architecture
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
A type is a collection of values
Presentation transcript:

Partitioning Patterns How to partition complex actors and concepts into multiple classes. Layered Initialization Filter Composite

PP - Layered Initialization Synopsis: You need multiple implementations with common logic in super and specialized in subs. However the common logic decides which specialized subclass to create. Therefore layered initialization encapsulates common and specialized logic to create the multiple implementations.

PP - Layered Initialization Context: You have a piece of logic that requires partial execution prior to determining which subclass methods might be used. You need to layer the initializations of the objects to process the complex logic or complex data.

PP - Layered Initialization Forces: A specialized class must be chosen to process complex data. Constructor of the specialized classes and their sub classes are invoked after it has been decided which specialized class to instanciate. Solution: Essence of this pattern is to layer the initializations of the objects participating in the pattern.

PP - Layered Initialization 1.Objects that performs logic common to all cases is initialized 2.Initialization concludes by determining the class to instantiate 3.Specialized class constructor performs next layer of initialization logic. 4.After all initialization, one top-level object exist for logic 5.If method needs more specialized logic, it calls method one layer down Consequences: Complexity of initialization of objects using data requires analysis before initialization can proceed.

Resolve Business Rules query request PP - Layered Initialization trigger to resolve business rule Suppose you have a business rule engine which must select a type of database on which to query to resolve issues in the rule base. Select Database Perform Oracle Query Perform DB2 Query Perform n Query Oracle query DB2 query n query You cannot perform query until you know what type of database

Resolve Business Rules query request PP - Layered Initialization trigger to resolve business rule You need to initialize the database prior to query. Select Database Perform Oracle Query Perform DB2 Query Perform n Query Oracle query DB2 query n query Initialize Database

PP - Layered Initialization DataQueryFactoryIF DataQuery request creation DataQueryImplFactory OracleQueryDB2Query….. DataQueryImplIF creates uses Data Query factory method object appears like this. ServiceImpFactoryIF ServiceImpFactory Service ServiceImpIIF

PP - Layered Initialization import java.util.Hashtable; // Factory Method class for creating instances of classes for database queries. class MyDataQueryFactory implements DataQueryFactoryIF { private static Hashtable classes = new Hashtable(); // populate the classes hashtable static { classes.put("INVENTORY", dataQuery.OracleQuery.class); classes.put("SALES", dataQuery.SybaseQuery.class); classes.put("PERSONNEL", dataQuery.OracleQuery.class); classes.put("WHEATHER", dataQuery.JDBCQuery.class); //... }

PP - Layered Initialization // Create a DataQueryImplIF object that retrieves data from the specified database. dbName the name of the database that will be queried An instance of a class specific to either JDBC or the physical database engine. // If the specified database is not know to this method, it returns null. public DataQueryFactoryIF createDataQueryImpl(String dbName) { Class clazz = (Class)classes.get(dbName); try { return (DataQueryFactoryIF)clazz.newInstance(); } catch (Exception e) { return null; } // try } // createDataQueryImpl(String) } // class MyDataQueryFactory

PP - Layered Initialization package dataQuery; // This class takes a database query and returns a result. public class DataQuery { // Factory object for creating DataQueryImplIF objects. private DataQueryFactoryIF factory; // Set the factory object Error if this method is called after a factory has been set public void setFactory(DataQueryFactoryIF factory) { if (this.factory != null) throw new Error("Data query factory already defined"); this.factory = factory; } // setFactory(DataQueryFactoryIF)

PP - Layered Initialization / Constructor query A string containing the query public DataQuery(String query) { //... while ( /*...*/ ) { String dbName = null; //... // Construct a database specific query object DataQueryImplIF dq; dq = (DataQueryImplIF)factory.createDataQueryImpl(dbName); //... } // while //... } // Constructor(String) //... } // class DataQuery

PP - Layered Initialization package dataQuery; // Factory classes that create instances of classes that implement the // DataQueryImplIF interface must implement this interface. public interface DataQueryFactoryIF { // Create a DataQueryImplIF object that retrieves data from the specified database. dbName - name of database that will be queried An instance of a class specific to either JDBC or the physical database engine. public DataQueryFactoryIF createDataQueryImpl(String dbName); } // DataQueryFactoryIF

PP - Layered Initialization // Constructor query A string containing the query public DataQuery(String query) { //... while ( /*...*/ ) { String dbName = null; //... // Construct a database specific query object DataQueryImplIF dq; dq = (DataQueryImplIF)factory.createDataQueryImpl(dbName); //... } // while //... } // Constructor(String) //... } // class DataQuery

PP - Layered Initialization package dataQuery; // Classes the perform data queries on data bases implement this interface. interface DataQueryImplIF { //... } // interface DataQueryImplIF // Class to perform queries using JDBC class JDBCQuery implements DataQueryImplIF { //... } // class JDBCQuery

PP - Layered Initialization package dataQuery; // Class to perform queries against an Oracle Database. class OracleQuery implements DataQueryImplIF { //... } // class OracleQuery // Class to perform queries against an Sybase Database. class SybaseQuery implements DataQueryImplIF { //... } // class SybaseQuery

PP - Filter Synopsis: Allows objects that perform different transformation and computations on streams of data and that have compatible interfaces to dynamically connect in order to perform arbitrary operations on streams of data.

PP - Filter Context: Define classes that perform the more common transformations and analysis. Solution: Filter pattern organizes classes that participate in it as data sources, sinks and filters. They perform transformation and analysis operations. 1.Data flows as a result of data sink object calling a method in a data source object 2.Data flows when a data source object passes data to a method of a sink object. Consequences: Filter pattern is structured as a set of sources, sinks and filters.

Organize Lines lines Sink input Define the classes necessary to perform the transformation. PP - Filter Source Tranform Data output Sink input Source For any general transformation.

AbstractSink PP - Filter AbstractSourceFilter ConcreteSourceFilter AbstractSource ConcreteSource Source Filter gets AbstractSink

AbstractSource PP - Filter AbstractSinkFilter ConcreteSinkFilter AbstractSink ConcreteSink Sink Filter gets

import java.io.IOException; // Filter class to count the number of bytes read from an InStream public class ByteCountInStream extends FilterInStream { private long byteCount = 0; // inStream InStream this object should delegate read operations to. public ByteCountInStream(InStream inStream) throws IOException { super(inStream); } // Constructor(InStream) PP - Filter

// Read bytes from a stream of bytes and fill an array with those bytes. array The array of bytes to fill. IOException if a I/O error occurs. public int read(byte[] array) throws IOException { int count; count = super.read(array); if (count >0) byteCount += count; return count; } // read(byte[]) // return the number of bytes that have been read by this object. public long getByteCount() { return byteCount; } // getByteCount() } // class ByteCountInStream PP - Filter

import java.io.IOException; import java.io.RandomAccessFile; // This class reads a stream of bytes from a file. public class FileInStream extends InStream { private RandomAccessFile file; // fName The name of the file to read public FileInStream(String fName) throws IOException { file = new RandomAccessFile(fName, "r"); } // Constructor(String) PP - Filter

// Read bytes from a file and fill an array with those bytes. array The array of bytes to fill. If not enough bytes available to fill array this method returns after partial fill. // This methods returns -1 if the end of the data stream is encountered. IOException if a I/O error occurs. public int read(byte[] array) throws IOException { return file.read(array); } // read(byte[]) } // class FileInStream PP - Filter

import java.io.IOException; // Abstract filter class for InStream objects. // This class does no actual tranformation or analysis of data. // It just provides a read method that delegates the actual read to another InStream object. public class FilterInStream extends InStream { private InStream inStream; // inStream InStream this object should delegate read operations to. public FilterInStream(InStream inStream) throws IOException { this.inStream = inStream; } // Constructor(InStream) PP - Filter

// Read bytes from a stream of bytes and fill an array with those bytes. array The array of bytes to fill. IOException if a I/O error occurs. public int read(byte[] array) throws IOException { return inStream.read(array); } // read(byte[]) } // class FilterInStream PP - Filter

import java.io.IOException; // Abstract class for reading a stream of bytes into an byte[]. public abstract class InStream { // Read bytes and fill an array with those bytes. array The array of bytes to fill. If not enough bytes available to fill the array method returns after partial fill. // This methods returns -1 if the end of the data stream is encountered. IOException if a I/O error occurs. public abstract int read(byte[] array) throws IOException; } // class InStream PP - Filter

import java.io.IOException; // Filter class to perform eight bit character translation. // This class treats bytes in bytes stream as eight bit character codes. // It then translates with a translation table. public class TranslateInStream extends FilterInStream { private byte[] translationTable; private final static int TRANS_TBL_LENGTH = 256; PP - Filter

// Constructor inStream The InStream that this objects should delegate read operations to table Array of bytes used to determine translation values for character codes. // Value to replace character code. If shorter than length then no translation. // If array longer than TRANS_TBL_LENGTH additional elements are ignored. public TranslateInStream(InStream inStream, byte[] table) throws IOException { super(inStream); // Create translation table by copying translation data. translationTable = new byte[TRANS_TBL_LENGTH]; System.arraycopy(table, 0, translationTable, 0, Math.min(TRANS_TBL_LENGTH, table.length)); for (int i = table.length; i < TRANS_TBL_LENGTH; i++) { translationTable[i] = (byte)i; } // for } // Constructor(InStream) PP - Filter

// Read bytes from a stream of bytes and fill an array with those bytes. array The array of bytes to fill. IOException if a I/O error occurs. public int read(byte[] array) throws IOException { int count; count = super.read(array); for (int i = 0; i < count; i++) { array[i] = translationTable[array[i]]; } // for return count; } // read(byte[]) } // class ByteCountInStream PP - Filter

PP - Composite Alias: Recursive Composition Synopsis: Recursively building a composite object from other objects. Allows you to build complex objects by recursively composing similar objects in a treelike manner. Allows objects in the tree to be manipulated in a consistent manner, by requiring all of the objects in the tree to have a common superclass or interface.

PP - Composite Alias: Recursive Composition Context: There is a fair amount of complexity involved in these objects. Composite pattern removes that complexity by allowing these objects to know how to handle the complexity. Forces: You have a complex object that you want to decompose into a part whole hierarchy of objects.

Solution: Minimize the complexity of a composite object organized into part whole hierarchies by providing an abstract superclass for all objects in the hierarchy and an abstract superclass for all composition in the hierarchy. Consequences: High cohesion may arise as a result of applying this pattern. PP - Composite

Format Document formated You need to create a structure of the objects that comprise the formatted document. characters, gifs, frames Suppose you have characters, gifs, and frames and you wish to combine them into lines and columns. PP - Composite

DocumentElement edits PP - Composite CharacterImage CompositeDocumentElement DocumentPageColumnFrameLineofText AbstractComponent ConcreteComponent Abstract Composite ConcreteComposite