Highlights of Previous Lecture

Similar documents
Ingegneria del Software Corso di Laurea in Informatica per il Management. Software quality and Object Oriented Principles

Principles of Object-Oriented Design

COURSE 2 DESIGN PATTERNS

OO Class Design Principles

Highlights of Last Week

17.11 Bean Rules persistent

Bruno Bossola SOLID Design Principles

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

More Relationships Between Classes

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

OO Design Principles

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Software Engineering

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Why Design by Contract! CS 619 Introduction to OO Design and Development. Design by Contract. Fall 2012

Highlights of Previous Lecture

Principles of OO Design

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Open Closed Principle (OCP)

First IS-A Relationship: Inheritance

SOLID: Principles of OOD

Conception Orientée Objets. Programmation SOLID

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

INSTRUCTIONS TO CANDIDATES

Object-Oriented Design I - SOLID

Software Construction

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

Abstract Classes and Interfaces

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

CS 520 Theory and Practice of Software Engineering Fall 2018

Exam in TDDB84: Design Patterns,

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CSE 70 Final Exam Fall 2009

Single Responsibility Principle (SRP)

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER. Are we there yet?

Java Review: Objects

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

Abstraction. Design fundamentals in OO Systems. Fundamental Software Development Principles

Exceptions and Design

Design Principles: Part 2

Object-Oriented Design

Sets and Maps. Part of the Collections Framework

Exercise: Singleton 1

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

CS 520 Theory and Practice of Software Engineering Fall 2017

Chapter 11: Collections and Maps

Design Principles: Part 2

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Inheritance & Polymorphism

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

CSC207H: Software Design SOLID. CSC207 Winter 2018

About This Lecture. Outline. Handling Unusual Situations. Reacting to errors. Exceptions

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

What is the Java Collections Framework?

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

Object Fundamentals Part Three. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 4 09/06/2007

Lecture 4: Extending Classes. Concept

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

Object-oriented design principles

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Motivations. Chapter 13 Abstract Classes and Interfaces

Inheritance, Polymorphism, and Interfaces

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Highlights of Last Week

CSE 331 Software Design and Implementation. Lecture 17 Events, Listeners, Callbacks

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Programming II (CS300)

Array Based Lists. Collections

CSE 341, Autumn 2015, Ruby Introduction Summary

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

School of Informatics, University of Edinburgh

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CSC207 Week 3. Larry Zhang

9/16/2010 CS Ananda Gunawardena

Design Patterns: State, Bridge, Visitor

Object Oriented Programming. Java-Lecture 11 Polymorphism

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

CSE 331 Software Design and Implementation. Lecture 16 Callbacks and Observers

20 Years of. Improve the Design of your Code. Dr Dimitris Dranidis JAVA Meetup Group, Thessaloniki May 2015

Options for User Input

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CS Ananda Gunawardena

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

WA1278 Introduction to Java Using Eclipse

Collections. The Java Collections Framework. The Interfaces

Object design. François Schwarzentruber ENS Cachan Antenne de Bretagne

C16b: Exception Handling

Programming II (CS300)

Software Engineering Design & Construction

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CSE 331 Final Exam 3/16/15 Sample Solution

Lecture 4. Types, Memory, Exceptions

Transcription:

Highlights of Previous Lecture Final Project Goals 1. Set up collections of Flights 2. Maintain information about reservation availability on flights 3. Respond to reservation requests 4. Set up collections of Reservations on Flights 5. Support the Flight and Reservation interfaces so that my code can invoke methods defined by them. Designing Objects / Object Semantics Designing Object Fields and Methods 1

Project issues 2

Declaring your own Exception classes file:../week04/unknownstudentexception.java /** This exception is thrown when a student or student identifier does ** not correspond to a real student. **/ public class UnknownStudentException extends Exception { public UnknownStudentException() { public UnknownStudentException(String msg) { super(msg); 3

Using Exceptions Used: as in../week04/cscourse.java /** Assign a homework grade to the student identfied by id for ** the specified assignment number. **/ public void sethomeworkgrade( Object id, int assignment, String grade) throws UnknownStudentException { // Find the student object whose id is id CSStudent s = (CSStudent) students.get(id); if (s!= null) { s.assignhomeworkgrade(assignment, grade); else { throw new UnknownStudentException(id.toString()); And, in../week04/cstest.java // Exercise the exception handling String fakeid = "000-00-0000"; try { cs332.sethomeworkgrade(fakeid, 1, "A"); catch (UnknownStudentException e) { System.err.println(e); // Or something more 4

Software Systems Change - Design for It Most systems undergo numerous changes after their first delivery. Any model of software development that only considers the period leading to that delivery and ignores the subsequent era of change and revision is as remote from real life as those novels which end when the hero marries the heroine -- the time which, as everyone knows, marks the beginning of the really interesting part. -- Bertrand Meyer Symptoms of a Bad Design* Rigidity - software that is difficult to change. Every change causes a cascade of other changes to be required Fragility - software that breaks every time something is changed Breakage occurs in places that (should) have little or no relationship to where the change occurred. Immobility - inability to reuse software from other projects or from parts of the same project. Modules contain too much unnecessary baggage. Viscosity of design - when faced with the need to make a change you have two choices: changes that preserve the design and changes that are just hacks to get the change accomplished. Viscosity of design is high when... the hacks are easier than the design preserving changes.... "it is easy to do the wrong thing, but hard to do the right thing." 5

* - from Robert C. Martin - Design Principles and Design Patterns http://www.objectmentor.com/publications/principles%20and%20patterns.pdf 6

Principles of Object Oriented Some approaches to object oriented implementation have been found to lead to better designs: The Open-Closed Principle - OCP - A class should be extensible without requiring modification The Liskov Substitution Principle - LSP - Derived classes should be substitutable for their base classes The Dependency Inversion Principle - DIP - Depend upon abstractions. Do not depend upon concretions The Interface Segregation Principle - ISP - Many client specific interfaces are better than one general purpose interface. 7

The Open-Closed Principle A module (class) should be open for extension but closed for modification. Classes should be written so that they can be extended without requiring the classes to be modified. Enables one to change what classes do without changing the classes source code. To extend the behavior of a system (in response to a requested change) add new code, don t modify existing code. Abstraction (and polymorphism) are the keys to the Open Closed Principle 8

Use Polymorphism "the ability to manipulate objects of distinct classes using only knowledge of their common properties without regard for their exact class." Using interfaces and abstract classes are the keys to polymorphism 9

Liskov Substitution Principle Subclasses should be substitutable for their base classes A user of a base class should still function if given an instance of a derived class instead. "A derived class should have some kind of specialized behavior (it should provide the same services as the superclass, only some, at least, are provided differently.)" The contract of the base class must be honored by the derived class. If this principle is violated, then you will cause the Open-Closed Principle to be violated also. Why? 10

The Dependency Inversion Principle Depend upon abstractions. Do not depend upon concretions High level classes should not depend on low level classes Abstractions should not depend upon the details If the high level abstractions depend on the low level implementation details, then the dependency is inverted from what it should be. High level classes contain the "business logic", low level classes are the details of the (current) implementation. Depend upon interfaces or abstract classes to avoid dependency inversion. 11

Procedural vs OO Dependency Typical, procedural style dependencies Correct Object Oriented Dependency If higher levels of the application are not going to depend on particular, low level implementations, how does one construct the necessary low level implementations? 12

The Interface Segregation Principle Many client specific interfaces are better than one general purpose interface. If you have a class with several different uses, create separate (narrow) interfaces for each use. The alternative is that all uses share the same, large interface (and most uses will need only a small number of the methods.) Instead the uses of the class can be organized as groups of related methods, each of which serves a different purpose. Each of these groups becomes a separate interface. The purpose is to make clients use as small and as coherent an interface as possible. Fat interfaces lead to inadvertent couplings between classes: dependencies that are accidental. 13

Java2 Collection Framework Interfaces - generic ways of treating different collections alike Abstract classes that provide starter / partial implementations Full implementations of some useful data structures Built-in implementations of efficient algorithms for sorting, searching, "bulk" operations: perform an operation on an entire Collection in a single method call, e.g., set operations. Consistent set of interfaces consistently implemented 14

Core Collections Interfaces Collection - an interface that defines methods for adding, removing, and querying a data structure List - an ordered collection, possibly containing duplicates, with methods for adding, removing, accessing a portion Set - a collection without duplicates, with methods for doing set operations: intersection, difference SortedSet - A set that guarantees that its iterator will traverse the set in ascending element order Map - for storing key/value pairs, with methods that to access keys, values, or key/value pairs SortedMap - A map that guarantees that it will be in ascending key order 15

Collection Framework Interfaces This illustrates the relationship between the Core Collection interfaces. 16

Collection Interface Implementations From Collections Framework Overview 17

Collections Examples Collections.sort(List l, Comparator c) Collections.binarySearch(List l, Object key, Comparator c) Collections.min(Collection c, Comparator c) ItineraryCollections.java import java.util.list; import java.util.collections; import java.util.comparator; import java.util.date; import reservations.airport; import reservations.itinerary; import reservations.itineraryreader; public class ItineraryCollections { private void createcollection(list list) { try { ItineraryReader reader = new ItineraryReader(); Itinerary i; while ((i = reader.nextitinerary())!= null) { list.add(i); // Itinerary will insist that they are registered // with a Flight before permitting any methods. new MinimalFlightImpl(i); catch (java.io.ioexception e) { System.err.println(e); System.out.println("list using " + list.getclass().getname()); /** This method will demonstrate the collection capabilities using ** Itinerary s natural ordering. **/ private void demoitinerarycollection(list list) { try { // This throws a ClassCastException (because the 18

// objects in the list do not implement the Comparable // interface.) Collections.sort(list); System.out.println("sorted:"); System.out.println(list); Object min = Collections.min(list); System.out.println("min:" + min); catch (ClassCastException e) { System.err.println("Bzzt! List elements do not support Comparable"); /** A helper class (functor) that defines an Itinerary ** ordering by starting airport code. **/ class OrderByStartingAirport implements Comparator { public int compare(object o1, Object o2) { if (o1 instanceof Itinerary && o2 instanceof Itinerary) { Itinerary i1 = (Itinerary) o1; Itinerary i2 = (Itinerary) o2; Airport a1 = i1.getstartingairport(); Airport a2 = i2.getstartingairport(); String s1 = a1.getcode(); String s2 = a2.getcode(); return s1.compareto(s2); else throw new ClassCastException("not a Itinerary"); public String tostring() { return "comparison by StartingAirport code"; /** This method will demonstrate the collection capabilities using ** Itinerary s order specified by the above helper class **/ private void demosaitinerarycollection(list list) { OrderByStartingAirport comparer = new OrderByStartingAirport(); Collections.sort(list, comparer); System.out.println("sorted "+ comparer); System.out.println(list); Object min = Collections.min(list, comparer); System.out.println("min:" + min); 19

/** A helper class (functor) that defines an Itinerary ** ordering by Destination airport city name. **/ class OrderByDestinationAirport implements Comparator { public int compare(object o1, Object o2) { if (o1 instanceof Itinerary && o2 instanceof Itinerary) { Itinerary i1 = (Itinerary) o1; Itinerary i2 = (Itinerary) o2; Airport a1 = i1.getdestinationairport(); Airport a2 = i2.getdestinationairport(); String s1 = a1.getcity(); String s2 = a2.getcity(); return s1.compareto(s2); else throw new ClassCastException("not a Itinerary"); public String tostring() { return "comparison by Destination Airport city"; /** This method will demonstrate the collection capabilities using ** Itinerary s order specified by the above helper class **/ private void demodcitinerarycollection(list list) { OrderByDestinationAirport comparer = new OrderByDestinationAirport(); Collections.sort(list, comparer); System.out.println("sorted "+ comparer); System.out.println(list); Object min = Collections.min(list, comparer); System.out.println("min:" + min); /** A helper class (functor) that defines an Itinerary ** ordering by departure time. **/ class OrderByDepartureTime implements Comparator { public int compare(object o1, Object o2) { if (o1 instanceof Itinerary && o2 instanceof Itinerary) { Itinerary i1 = (Itinerary) o1; Itinerary i2 = (Itinerary) o2; Date d1 = i1.getdeparturetime(); Date d2 = i2.getdeparturetime(); 20

return d1.compareto(d2); else throw new ClassCastException("not a Itinerary"); public String tostring() { return "comparison by Departure Time"; /** This method will demonstrate the collection capabilities using ** Itinerary s order specified by the above helper class **/ private void demodtitinerarycollection(list list) { OrderByDepartureTime comparer = new OrderByDepartureTime(); Collections.sort(list, comparer); System.out.println("sorted "+ comparer); System.out.println(list); Object min = Collections.min(list, comparer); System.out.println("min:" + min); public static void main(string[] args) { ItineraryCollections c = new ItineraryCollections(); List list; if (args.length == 0) list = new java.util.arraylist(); else list = new java.util.linkedlist(); c.createcollection(list); c.demoitinerarycollection(list); c.demosaitinerarycollection(list); c.demodcitinerarycollection(list); c.demodtitinerarycollection(list); 21

Homework Final Project 22