CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 2, Week 4 Design by Contract, Java Exceptions, Eventing, Finding the Classes,

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Chapter 11 Component-Level Design
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exceptions and Exception Handling Carl Alphonce CSE116.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Software Testing and Quality Assurance
Design Patterns CS is not simply about programming
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 10 Classes Continued
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Design Patterns Introduction. What is a Design Pattern?  A technique to repeat designer success.  Borrowed from Civil and Electrical Engineering domains.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
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.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
CSC 243 – Java Programming, Spring, 2014 March 2014 Week 6ish, Exceptions.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
CSC 243 – Java Programming, Spring, 2009 Week 9 Java’s Event Infrastructure.
Computer Science Projects Internal Assessment. Mastery Item Claimed Justification Where Listed Random Access File – Searching Lines P. 53 Random.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS 151: Object-Oriented Design October 15 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Java Beans - Basics CIS 421 Web-based Java Programming.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Introduction to UML and Rational Rose UML - Unified Modeling Language Rational Rose 98 - a GUI tool to systematically develop software through the following.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 32 JavaBeans and Bean Events
Data Abstraction: The Walls
Chapter 36 JavaBeans and Bean Events
Introduction to Design Patterns
Observer Design Pattern
Observer Design Pattern
Object-Oriented Programming
Component-Level Design
Exception Handling Chapter 9 Edited by JJ.
Copyright 2007 Oxford Consulting, Ltd
Engineering Quality Software
CMSC 202 Exceptions.
CSC 243 – Java Programming, Fall, 2008
SDMX IT Tools SDMX Registry
Presentation transcript:

CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 2, Week 4 Design by Contract, Java Exceptions, Eventing, Finding the Classes, Design Patterns and the Kitchen Sink

Design by Contract Preconditions – constraints on the caller Postconditions – constraints on the called Invariants – constraints on an object’s state Preconditions may weaken for derived class methods that override a base class or interface method. Why??? Postconditions may strengthen for derived class methods that override a base class or interface method. Why???

Exception constructs already in use try { attempt to run a block of code } catch (Type_A_Exception taex) { Do something with taex, e.g., taex.getMessage() } catch (Type_A_Exception tbex) { Do something with tbex, e.g., print, then throw tbex } finally { Do this after all of the above, even after a return! }

Unchecked and checked exceptions javac forces client code to catch checked exceptions unchecked exceptions need not be caught; they can be – java.lang.Error java.io.IOError – maybe a disk drive goes off line – java.lang.RuntimeException java.lang.NullPointerException Other java.lang.Exceptions are checked

Throwing a new exception An explicit throw creates a new Exception. – public int deleteTiles(String tileset) throws ScrabbleException throw new ScrabbleException("Player " + name + " does not have " + tile.toString() + " to delete from set of tiles.");

Rethrowing an exception A rethrow catches and handles an Exception object, then throws it again. } catch (NumberFormatException nx) { System.err.println(“Exception: “ + nx.getMessage(); nx.printStackTrace(); // to System.err throw nx ; // This is the rethrow

An implicit throw An implicit throw allows a called method to throw an Exception via the calling method. – public String [][] move(String command) throws ScrabbleException Invokes “int used = players[nextPlayer].deleteTiles(validword); But move() does not catch deleteTiles’s exception: – public int deleteTiles(String tileset) throws ScrabbleException The remove() implicitly throws deleteTile’s exception.

A Chained Exception A chained Exceptions tacks a detail message onto an underlying cause Exception. – } catch (NumberFormatException nx) { – throw new Exception(“detail message”, nx); Used to prepend a context-specific message to an Exception thrown from a called method. The new Exception must have the appropriate constructor. It may be a custom Exception.

Custom Exceptions A custom Exception inherits, directly or indirectly, from java.lang.Exception. – public class ScrabbleException extends Exception public ScrabbleException(String text) – super(text); // Call to base class constructor. Client code can explicitly catch a custom Exception.

An event mechanism = publishers, subscribers & delivered events java.util.EventListener is a tagging interface that applications implement to create a listener whose class identity is unknown to the publisher java.awt.event.ActionListener for GUIs java.beans.beancontext.BeanContext for Java Bean tools that automated assembly of components via graphical tools games2010rev5.GameMoveEventListener

Publishers Publish Events Publisher classes support subscribe and unsubscribe methods via which their Event Listeners (see previous slide) can subscribe and unsubscribe for event delivery. Publishers later deliver events to subscribed listeners when the events occur. There is no standard interface or base class for publishers in the Java library. Any class that creates and delivers an event type is a publisher.

Event Objects Carry Data java.util.EventObject is the library base class getSource() returns Object source of the event – Subclasses use this base class to store the event publisher java.awt.event.ActionEvent for GUIs java.beans.beancontext.BeanContextEvent games2010rev5.GameMoveEvent – getDescription(), getGameState(), getMoveNumber() Publisher passes data contents to the event constructor – Event class provides getters for these data javax.sound.midi.MidiEvent does not implement it

UML Sequence Diagram for Event Subscription Lifecycle TwoDimensionalBoardGameGameMoveEventListener subscribeToGameMoveEvent notifyMoveEvent(event:GameMoveEvent) (0..* times) unsubscribeToGameMoveEvent

Observer Design Pattern Gang of Four calls this the Observer Pattern “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.” The official design pattern does not show the event object, which typically passes as a parameter to the event listener-defined method.

Meyer and Design Patterns 22.3 General Heuristics for Finding Classes An analysis class describes a data abstraction directly drawn from the model of the external system. ScrabbleBoard An implementation class describes a data abstraction introduced for the internal needs of the algorithms in the software. private char [][] tiles = new char [RowHeadings.length][ColumnHeadings.length]; A design class describes an architectural choice. Design patterns.

How to Find the Classes Tell stories. Underline the nouns. Adjectives for deferred classes (comparable, serializable). Watch out for natural language nouns! Avoid useless classes. Avoid Blobs and Grand Central Stations. Elicit, then reject classes. (Edit!) Previous designs and class libraries. Meyer uses use cases only for validation, not analysis. He does not like CRC cards We use class diagrams.

Some other Design Patterns for Tonight -- Factories Abstract Factory – our map_interface The manufacturing interface is abstract. The manufactured object interface is abstract. A concrete class derived from the manufacturing interface constructs concrete objects derived from the manufactured object interface. Factory Method The manufacturing class has an abstract manufacturing method. Its subclasses manufacture concrete class objects.

Memento Pattern Without violating encapsulation, capture and externalize an object’s state so that the object can be restored to this state later. java.lang.Cloneable, java.io.Serializable See Scrabble class data field definitions. See ScrabbleGame save and restore methods.

Iterator Pattern Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. An iterator is an abstracted pointer. See interface java.util.Iterator and ListIterator. C++ Standard Template Library supports forward and reverse iterators.