2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.1 CSC7322: Object Oriented Development J Paul Gibson, A207

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/FundamentalConcepts.1 CSC7322: Object Oriented Development J Paul Gibson, A207
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Identity and Equality Based on material by Michael Ernst, University of Washington.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
13-Jul-15 Abstract Data Types. 2 Data types I We type data--classify it into various categories-- such as int, boolean, String, Applet A data type represents.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Object Oriented Design and UML
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/DesignII.1 TO DO: Create a new class which adds statistics to the dice This class should add.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Chapter 18 Java Collections Framework
10-Nov-15 Java Object Oriented Programming What is it?
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Software Development
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Interfaces and Inner Classes
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Polymorphism 1. Reuse of code: every time a new sub-class is defined, programmers are reusing the code in a super-class. All non-private members of a.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
The need for Programming Languages
Lecture 12 Inheritance.
Software Development Java Classes and Methods
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Abstract Data Types 24-Feb-19.
Object-Oriented Programming
Java Programming Language
Inheritance Lakshmish Ramaswamy.
CS 240 – Advanced Programming Concepts
Presentation transcript:

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.1 CSC7322: Object Oriented Development J Paul Gibson, A207 Design (In Java) …/~gibson/Teaching/CSC7322/L3-Design(In Java).pdf

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.2 There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies. —C.A.R. Hoare DISCUSSION: What is (software) design?

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.3 Some reading material on design (in general) Domain-Driven Design: Tackling Complexity in the Heart of Software, Eric Evans, 2003 Foundations for the Study of Software Architecture, Dewayne E. Perry and Alexander L. Wolf, 1992 A guided tour of program design methodologies, Bergland, Glenn D., On the criteria to be used in decomposing systems into modules, Parnas, David Lorge, 1972 Notes on structured programming. Dijkstra, E.W., The pragmatics of model-driven development, Selic, Bran. 2003

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.4 Design: the bridge between the problem and the solution Discussion: How often does software fail because the design bridge collapses?

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.5 Good design is all about experience: Recognising a problem’s structure and knowing about alternative solutions (designs) to meeting the requirements of the problem Knowing about implementation issues specific to potential implementation languages/architectures Realising that there is often not a perfect fit between the design and the target implementation language/architecture Being able to adapt the design to the language and/or adapt the language to the design Note: in OO we may consider the library to be part of the language Let us return to the Dice problem with this in mind. Good judgement is the result of experience … Experience is the result of bad judgement. —Fred Brooks

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.6 From the previous lecture: the Dice problem –

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.7 From the previous lecture: the Dice problem – public interface DiceSpecification extends HasInvariant{ /** * The minimum number of sides for a Dice as specified in the requirements */ final static int MINIMUM_numberOfSides = 3; /** * The maximum number of sides for a Dice as specified in the requirements */ final static int MAXIMIM_numberOfSides = 36; /** * The default number of sides for a Dice as specified in the requirements */ final static int DEFAULT_numberOfSides = 6;

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.8 From the previous lecture: the Dice problem – /** * True if the MAXIMIM_numberOfSides, MINIMUM_numberOfSides and * DEFAULT_numberOfSides values for the number of sides of the Dice * are coherent/valid, * otherwise false DiceSpecification#invariant() */ boolean INVARIANT_OF_CLASS = ( MAXIMIM_numberOfSides >= MINIMUM_numberOfSides && MINIMUM_numberOfSides >0 && DEFAULT_numberOfSides >= MINIMUM_numberOfSides && DEFAULT_numberOfSides <= MAXIMIM_numberOfSides);

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.9 From the previous lecture: the Dice problem – /** * Check that the Dice object is in a safe (meaningful) state * true if * * once the Dice is rolled, the last roll is within range * 1... NUMBEROFSIDES, or * if the Dice is yet to be rolled then the last roll value is zero, * and * the numberOfRolls is non-negative, and * the number of sides of dice is within range * MINIMUM_numberOfSides... MAXIMUM_numberOfSides * * otherwise false * DiceSpecification#INVARIANT_OF_CLASS */ boolean invariant();

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.10 From the previous lecture: the Dice problem – /** * Getter method for NUMBEROFSIDES * the number of sides of the Dice */ int numberOfSides(); /** * Getter method for numberOfRolls * the number of times the Dice has been rolled since it was constructed */ int numberOfRolls(); /** * Getter method for reading the value of the lastRoll * the lastRoll value */ int lastRoll ();

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.11 From the previous lecture: the Dice problem – /** * Updates the last roll to a random value between 1 and the number of sides, * and increments the roll count. * InvariantBroken if Dice is no longer in a safe state */ void roll() throws InvariantBroken;

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.12 From the previous lecture: the Dice problem – /** The current state of the Dice as a String * The format to be followed is, eg: * 6-sided Dice - lastRoll = 1. (Total number of rolls = 10) * */ public String toString();

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.13 From the previous lecture: the Dice problem – /** thing is the Object to test for equality true if * * the two objects reference the same address, or * the thing is a Dice and there is equality on * lastRoll and NUMBEROFSIDES fields * * otherwise false */ public boolean equals(Object thing);

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.14 From the previous lecture: the Dice problem – /** thing is the Object to test for equality true if * * the two objects reference the same address, or * the thing is a Dice and there is equality on * lastRoll and NUMBEROFSIDES fields * * otherwise false */ public boolean equals(Object thing); NOTE: Don’t forget to over-ride the hashCode method – If two objects are equal by equals() method then the result returned by the hashCode() method must be same. Multiple invocations on same object (that doesnt change state) must return the same result during a single execution; but can differ between executions (!)

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.15 From the previous lecture: the Dice problem –

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.16 From the previous lecture: the Dice problem – public abstract class DiceAbstraction implements DiceSpecification{ public boolean equals( Object thing){ if (thing ==null) return false; if ( this == thing) return true; if (! (thing instanceof DiceAbstraction)) return false; DiceAbstraction that = (DiceAbstraction) thing; return ( (this.lastRoll() == that.lastRoll()) && (this.numberOfSides() == that.numberOfSides()) ); } public String toString(){ String str =""; str = str+ numberOfSides()+"-sided Dice - lastRoll = "+lastRoll()+ ". (Total number of rolls = "+numberOfRolls()+")"; return str; }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.17 From the previous lecture: the Dice problem – /** * Algorithm taken from Josh Bloch's "Effective Java", using primes 37 and 41 * * initialise with prime = 37 * for each field tested in DiceAbstraction#equals} increase * iteratively with another prime multiplier: * hash = 41 * hash + field_integer_value * */ public int hashCode(){ int hash = 37; // start with a prime hash = 41 * hash + lastRoll(); // iteratively increase with another prime multiplier hash = 41 * hash + numberOfSides(); return hash; }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.18 From the previous lecture: the Dice problem – public boolean invariant(){ return (( (numberOfRolls() == 0 && lastRoll() == 0 ) || (numberOfRolls() > 0 && lastRoll()>0 && lastRoll()<=numberOfSides() ) )&& (numberOfSides()>=MINIMUM_numberOfSides) && (numberOfSides() <= MAXIMIM_numberOfSides) ); }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.19 From the previous lecture: the Dice problem –

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.20 From the previous lecture: the Dice problem – public class Dice extends DiceAbstraction implements DiceSpecification{ // The javadocs are in the code – removed here for easier presentation public final int NUMBEROFSIDES; protected Random rng = new Random(); protected static int numberOfDie = 0; protected int numberOfRolls = 0; protected int lastRoll = 0; public boolean invariant(){ return super.invariant() && rng !=null; } // We saw constructors in previous lecture

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.21 From the previous lecture: the Dice problem – public int lastRoll (){ return lastRoll; } public int numberOfSides(){ return NUMBEROFSIDES; } public int numberOfRolls(){ return numberOfRolls; } /** * Getter method for the static numberOfDie the number of Die that have been constructed */ public static int numberOfDie(){ return numberOfDie; } QUESTION : Why is the static method not specified in the interface or abstract class?

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.22 From the previous lecture: the Dice problem – /** * Setter method for the random number generator local to the dice * * Tested by JUnit_DiceTest#testSetRNGException}, which shows that * we do not need to check invariant as any invalid change to rng is caught * rng is the new random number generator to be used when rolling the dice IllegalArgumentException if argument rng is null */ public void setRNG( Random rng) throws IllegalArgumentException{ if (rng==null) throw (new IllegalArgumentException("Cannot set rng to null")); this.rng = rng; // if (!invariant()) throw (new InvariantBroken("Dice is no longer in a safe state")); } /** * Tested by JUnit_DiceAbstractionTest#testRoll}, which guarantees that the Dice is * constructed in a safe state as specified by Dice#invariant}. * */ public void roll() throws InvariantBroken{ lastRoll = rng.nextInt(NUMBEROFSIDES)+1; numberOfRolls++; // if (!invariant()) throw (new InvariantBroken("Dice is no longer in a safe state")); }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.23 From the previous lecture: the Dice problem public abstract void setUp() throws public abstract void tearDown() throws Exception;

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.24 From the previous lecture: the Dice problem – public class JUnit_DiceTest extends public void setUp() throws Exception { diceDefault = new Dice(); diceNonDefaultOK = new Dice((Dice.MAXIMIM_numberOfSides + Dice.MINIMUM_numberOfSides)/2); diceNonDefaultKO = new Dice(Dice.MAXIMIM_numberOfSides + 1); diceRolledTwice = new Dice(); diceRolledTwice.roll(); diceRolledTwice.roll(); diceCopyRolledTwice = new Dice ( (Dice) diceRolledTwice); }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.25 From the previous lecture: the Dice problem – /** * test exception in Dice#setRNG(java.util.Random)} = IllegalArgumentException.class) public void testSetRNGException() { ((Dice) diceDefault).setRNG(null); } /** * test Dice#numberOfDie()} by creating 10 new die and checking count public void testNumberOfDie() { int countNumberOfDie = Dice.numberOfDie(); final int NUMBER_OF_DIE_TO_CONSTRUCT = 10; for (int i=0; i<NUMBER_OF_DIE_TO_CONSTRUCT; i++) new Dice(); Assert.assertEquals(countNumberOfDie+NUMBER_OF_DIE_TO_CONSTRUCT, Dice.numberOfDie()); }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.26 From the previous lecture: the Dice problem – WARNING: All green does not always mean that all is well! DISCUSSION: Why not?

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.27 From the previous lecture: the Dice problem –

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.28 From the previous lecture: the Dice problem – public class Random_DiceTest { protected static Dice dice = new Dice(); public static void main(String[] args) { /** * The number of rolls in our simulation */ final int NUMBER_OF_TEST_ROLLS = 6; Random rng = SeedRNGCommandLine.getRandom(args); System.out.println(DateHeader.dateString()); System.out.println(dice); dice.setRNG(rng); System.out.println("Rolling "+NUMBER_OF_TEST_ROLLS+ " times"); for (int i =1; i<=NUMBER_OF_TEST_ROLLS;i++){ dice.roll(); System.out.println(i+". Roll = " +dice.lastRoll() ); } System.out.println(dice); }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.29 From the previous lecture: the Dice problem –

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.30 TO DO: Create a new class which adds statistics to the dice This class should add functionality to store the roll frequencies. You should implement a validation test (as well as running unit tests) as below: NOTE: Don’t forget to run regression tests

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.31 TO DO: Implement 2 different designs //using inheritance public class DiceWithStatistics1 extends Dice implements DiceWithStatisticsSpecification{ } //using composition public class DiceWithStatistics2 implements DiceWithStatisticsSpecification{ protected Dice dice; }

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.32 TO DO: Create 2 different terminal/console views The frequencies can either be displayed vertically or horizontally. If the frequency values are bigger than the height/width of the screen then you should scale them in order to occupy as much of the screen as possible. The screen size should be stored in constant variables HEIGHT and WIDTH. 1 |******** 2 |****** 3 |*** 4 |**** 5 |****** 6 |** 7 |*** 8 |** 9 |***** 10 | 11 |***** 12 |** 13 |******** 14 |***** 15 |********* * * * * ** * * * ** * * * *** ** ** * * *** ***** * * * *** ********* ***** DISCUSSION: How to best include these views in the design?

2013: J Paul GibsonTSP: Object Oriented DevelopmentCSC7322/Design.33 The consequences of the initial design decision– Choosing to implement a DiceWithStatistics as a subclass of Dice is a design decision that may have consequencies on later stages of the development. The alternative re-use mechanism was to design a DiceWithStatistics class/object to have a Dice component class/object. This also may have consequencies on later stages of the development DISCUSSION: What are the possible consequences (positive and negative)? CLUE: You may have seen them when we considered the different views OBSERVERS: We may wish to create different views that automatically update when the state of the models changes