1 / 33

CSC7322: Object Oriented Development J Paul Gibson, A207 paul.gibson@telecom-sudparis.eu

CSC7322: Object Oriented Development J Paul Gibson, A207 paul.gibson@telecom-sudparis.eu http:// www-public. telecom-sudparis.eu /~ gibson/Teaching/CSC7322/. Design (In Java) …/~ gibson / Teaching /CSC7322/L3-Design(In Java). pdf. DISCUSSION: What is (software) design?.

Download Presentation

CSC7322: Object Oriented Development J Paul Gibson, A207 paul.gibson@telecom-sudparis.eu

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC7322: Object OrientedDevelopment J Paul Gibson, A207 paul.gibson@telecom-sudparis.eu http://www-public.telecom-sudparis.eu/~gibson/Teaching/CSC7322/ Design (In Java) …/~gibson/Teaching/CSC7322/L3-Design(In Java).pdf TSP: Object OrientedDevelopment

  2. DISCUSSION: Whatis (software) design? 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 TSP: Object OrientedDevelopment

  3. Somereadingmaterial on design (in general) Notes on structuredprogramming. Dijkstra, E.W. , 1970. On the criteria to be used in decomposing systems into modules, Parnas, David Lorge, 1972 A guided tour of program design methodologies, Bergland, Glenn D. , 1981. Foundations for the Study of Software Architecture, Dewayne E. Perry and Alexander L. Wolf, 1992 The pragmatics of model-driven development, Selic, Bran. 2003 Domain-Driven Design: Tackling Complexity in the Heart of Software, Eric Evans, 2003 TSP: Object OrientedDevelopment

  4. Design: the bridge between the problem and the solution Discussion: How oftendoes software failbecause the design bridge collapses? TSP: Object OrientedDevelopment

  5. Good judgement is the result of experience … Experience is the result of bad judgement. —Fred Brooks • 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 potentialimplementationlanguages/architectures • Realisingthatthereisoften not a perfect fit between the design and the targetimplementationlanguage/architecture • Being able to adapt the design to the language and/or adapt the language to the design • Note: in OO wemayconsider the library to be part of the language • Let us return to the Diceproblemwiththis in mind. TSP: Object OrientedDevelopment

  6. From the previous lecture: the Diceproblem – TSP: Object OrientedDevelopment

  7. From the previous lecture: the Diceproblem – publicinterfaceDiceSpecificationextendsHasInvariant{ /** * The minimum number of sides for a Dice as specified in the requirements<br> */ finalstaticintMINIMUM_numberOfSides = 3; /** * The maximum number of sides for a Dice as specified in the requirements */ finalstaticintMAXIMIM_numberOfSides = 36; /** * The default number of sides for a Dice as specified in the requirements */ finalstaticintDEFAULT_numberOfSides = 6; TSP: Object OrientedDevelopment

  8. From the previous lecture: the Diceproblem – /** * True if the <code>MAXIMIM_numberOfSides</code>, <code>MINIMUM_numberOfSides</code> and * <code>DEFAULT_numberOfSides</code> values for the number of sides of the Dice * are coherent/valid, * otherwise false * @seeDiceSpecification#invariant() */ booleanINVARIANT_OF_CLASS = ( MAXIMIM_numberOfSides >= MINIMUM_numberOfSides && MINIMUM_numberOfSides >0 && DEFAULT_numberOfSides >= MINIMUM_numberOfSides && DEFAULT_numberOfSides <= MAXIMIM_numberOfSides); TSP: Object OrientedDevelopment

  9. From the previous lecture: the Diceproblem – /** * Check that the Dice object is in a safe (meaningful) state * * @returntrue if * <ul> * <li><ul><li> once the Dice is rolled, the last roll is within range * 1 ... <code> NUMBEROFSIDES </code></li>, or * <li> if the Dice is yet to be rolled then the last roll value is zero, </li> * </ul> and </li> * <li> the <code>numberOfRolls</code> is non-negative, and * </li><li> the number of sides of dice is within range * <code>MINIMUM_numberOfSides</code> ... <code>MAXIMUM_numberOfSides</code> *</li> * </ul> * otherwise false * * @seeDiceSpecification#INVARIANT_OF_CLASS */ boolean invariant(); TSP: Object OrientedDevelopment

  10. From the previous lecture: the Diceproblem – /** * Getter method for <code> NUMBEROFSIDES</code> * * @return the number of sides of the Dice */ intnumberOfSides(); /** * Getter method for <code>numberOfRolls</code> * * @return the number of times the Dice has been rolled since it was constructed */ intnumberOfRolls(); /** * Getter method for reading the value of the <code>lastRoll</code><br> * * @return the lastRoll value */ intlastRoll (); TSP: Object OrientedDevelopment

  11. From the previous lecture: the Diceproblem – /** * Updates the last roll to a random value between 1 and the number of sides, * and increments the roll count. * * @throwsInvariantBroken if Dice is no longer in a safe state */ void roll() throwsInvariantBroken; TSP: Object OrientedDevelopment

  12. From the previous lecture: the Diceproblem – /** * @return The current state of the Dice as a String<br> * The format to be followed is, eg:<br> * <pre> 6-sided Dice -lastRoll = 1. (Total number of rolls = 10) * </pre> */ public String toString(); TSP: Object OrientedDevelopment

  13. From the previous lecture: the Diceproblem – /** * @param thing is the Object to test for equality * @returntrue if * <ul> * <li> the two objects reference the same address, or * <li> the thing is a Dice and there is equality on * <code>lastRoll</code> and <code> NUMBEROFSIDES</code> fields * </ul> * otherwise false */ publicbooleanequals(Object thing); TSP: Object OrientedDevelopment

  14. From the previous lecture: the Diceproblem – /** * @param thing is the Object to test for equality * @returntrue if * <ul> * <li> the two objects reference the same address, or * <li> the thing is a Dice and there is equality on * <code>lastRoll</code> and <code> NUMBEROFSIDES</code> fields * </ul> * otherwise false */ publicbooleanequals(Object thing); NOTE: Don’tforget to over-ride the hashCodemethod – http://javarevisited.blogspot.fr/2011/10/override-hashcode-in-java-example.html If two objects are equal by equals() method then the result returned by the hashCode() method must be same. Multiple invocations on sameobject (thatdoesnt change state) must return the sameresultduring a single execution; but candifferbetweenexecutions (!) TSP: Object OrientedDevelopment

  15. From the previous lecture: the Diceproblem – TSP: Object OrientedDevelopment

  16. From the previous lecture: the Diceproblem – publicabstractclassDiceAbstractionimplementsDiceSpecification{ publicboolean equals( Object thing){ if (thing ==null) returnfalse; if ( this == thing) returntrue; if (! (thing instanceofDiceAbstraction)) returnfalse; DiceAbstractionthat = (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()+")"; returnstr; } TSP: Object OrientedDevelopment

  17. From the previous lecture: the Diceproblem – /** * Algorithm taken from Josh Bloch's "Effective Java", using primes 37 and 41 * <ul> * <li> initialise with prime = 37</li> * <li> for each field tested in {@link DiceAbstraction#equals} increase * iteratively with another prime multiplier: * hash = 41 * hash + field_integer_value</li> * </ul> * */ publicinthashCode(){ int hash = 37; // start with a prime hash = 41 * hash + lastRoll(); // iteratively increase with another prime multiplier hash = 41 * hash + numberOfSides(); returnhash; } TSP: Object OrientedDevelopment

  18. From the previous lecture: the Diceproblem – publicboolean invariant(){ return (( (numberOfRolls() == 0 && lastRoll() == 0 ) || (numberOfRolls() > 0 && lastRoll()>0 && lastRoll()<=numberOfSides() ) )&& (numberOfSides()>=MINIMUM_numberOfSides) && (numberOfSides() <= MAXIMIM_numberOfSides) ); } TSP: Object OrientedDevelopment

  19. From the previous lecture: the Diceproblem – TSP: Object OrientedDevelopment

  20. From the previous lecture: the Diceproblem – publicclassDiceextendsDiceAbstraction implementsDiceSpecification{ // The javadocs are in the code – removedhere for easierpresentation publicfinalintNUMBEROFSIDES; protected Random rng = new Random(); protectedstaticintnumberOfDie = 0; protectedintnumberOfRolls = 0; protectedintlastRoll = 0; publicboolean invariant(){ returnsuper.invariant() && rng !=null; } // Wesawconstructorsin previous lecture TSP: Object OrientedDevelopment

  21. From the previous lecture: the Diceproblem – publicintlastRoll (){ returnlastRoll; } publicintnumberOfSides(){ returnNUMBEROFSIDES; } publicintnumberOfRolls(){ returnnumberOfRolls; } /** * Getter method for the static<code>numberOfDie</code> * @return the number of Die that have been constructed */ publicstaticintnumberOfDie(){ returnnumberOfDie; } QUESTION : Whyis the staticmethod not specified in the interface or abstract class? TSP: Object OrientedDevelopment

  22. From the previous lecture: the Diceproblem – /** * Setter method for the random number generator local to the dice<br> * * Tested by {@link JUnit_DiceTest#testSetRNGException}, which shows that * we do not need to check invariant as any invalid change to rng is caught * * @paramrng is the new random number generator to be used when rolling the dice * @throwsIllegalArgumentException if argument <code>rng</code>is<code>null</code> */ publicvoidsetRNG( Random rng) throwsIllegalArgumentException{ if (rng==null) throw (newIllegalArgumentException("Cannot set rng to null")); this.rng = rng; // if (!invariant()) throw (new InvariantBroken("Dice is no longer in a safe state")); } /** * Tested by {@link JUnit_DiceAbstractionTest#testRoll}, which guarantees that the Dice is * constructed in a safe state as specified by {@link Dice#invariant}. * */ publicvoid roll() throwsInvariantBroken{ lastRoll = rng.nextInt(NUMBEROFSIDES)+1; numberOfRolls++; // if (!invariant()) throw (new InvariantBroken("Dice is no longer in a safe state")); } TSP: Object OrientedDevelopment

  23. From the previous lecture: the Diceproblem – @Before publicabstractvoidsetUp() throws Exception; @After publicabstractvoidtearDown() throws Exception; TSP: Object OrientedDevelopment

  24. From the previous lecture: the Diceproblem – publicclassJUnit_DiceTestextendsJUnit_DiceAbstractionTest @Before publicvoidsetUp() throws Exception { diceDefault = newDice(); diceNonDefaultOK = new Dice((Dice.MAXIMIM_numberOfSides + Dice.MINIMUM_numberOfSides)/2); diceNonDefaultKO = newDice(Dice.MAXIMIM_numberOfSides + 1); diceRolledTwice = newDice(); diceRolledTwice.roll(); diceRolledTwice.roll(); diceCopyRolledTwice = new Dice ( (Dice) diceRolledTwice); } TSP: Object OrientedDevelopment

  25. From the previous lecture: the Diceproblem – /** * test exception in {@link Dice#setRNG(java.util.Random)} */ @Test(expected = IllegalArgumentException.class) publicvoidtestSetRNGException() { ((Dice) diceDefault).setRNG(null); } /** * test {@link Dice#numberOfDie()} by creating 10 new die and checking count */ @Test publicvoidtestNumberOfDie() { intcountNumberOfDie = Dice.numberOfDie(); finalint NUMBER_OF_DIE_TO_CONSTRUCT = 10; for (inti=0; i<NUMBER_OF_DIE_TO_CONSTRUCT; i++) new Dice(); Assert.assertEquals(countNumberOfDie+NUMBER_OF_DIE_TO_CONSTRUCT, Dice.numberOfDie()); } TSP: Object OrientedDevelopment

  26. From the previous lecture: the Diceproblem – WARNING: All green does not alwaysmeanthat all iswell! DISCUSSION: Why not? TSP: Object OrientedDevelopment

  27. From the previous lecture: the Diceproblem – TSP: Object OrientedDevelopment

  28. From the previous lecture: the Diceproblem – publicclassRandom_DiceTest { protectedstaticDicedice = newDice(); publicstaticvoid main(String[] args) { /** * The number of rolls in our simulation */ finalint NUMBER_OF_TEST_ROLLS = 6; Randomrng = 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 (inti =1; i<=NUMBER_OF_TEST_ROLLS;i++){ dice.roll(); System.out.println(i+". Roll = " +dice.lastRoll() ); } System.out.println(dice); } } TSP: Object OrientedDevelopment

  29. From the previous lecture: the Diceproblem – TSP: Object OrientedDevelopment

  30. TO DO: Create a new class whichaddsstatistics to the dice This class shouldaddfunctionality to store the roll frequencies. You shouldimplement a validation test (as well as running unit tests) as below: NOTE: Don’tforget to runregression tests TSP: Object OrientedDevelopment

  31. TO DO: Implement 2 different designs //using inheritance publicclass DiceWithStatistics1 extends Dice implementsDiceWithStatisticsSpecification{ } //using composition publicclass DiceWithStatistics2 implementsDiceWithStatisticsSpecification{ protectedDicedice; } TSP: Object OrientedDevelopment

  32. TO DO: Create2 different terminal/console views The frequenciescaneitherbedisplayedvertically or horizontally. If the frequency values are biggerthan the height/width of the screenthenyoushouldscalethem in order to occupy as much of the screen as possible. The screen size shouldbestored in constant variables HEIGHT and WIDTH. 10.0 * * * * * * * * * * * * * * * * * * * * ** * * * ** * * * ** * * * *** ** ** * * *** ** ** * * *** ***** * * * *** ***** * * * *** ********* ***** ********* ***** ********* ***** ********* ***** ********* ***** 1.0 --------------- 123456789111111 012345 1 |******** 2 |****** 3 |*** 4 |**** 5 |****** 6 |** 7 |*** 8 |** 9 |***** 10 | 11 |***** 12 |** 13 |******** 14 |***** 15 |*********10.0 1.0 DISCUSSION: How to best includetheseviews in the design? TSP: Object OrientedDevelopment

  33. The consequences of the initial design decision– Choosing to implement a DiceWithStatistics as a subclass of Diceis a design decisionthatmayhave consequencies on later stages of the development. The alternative re-use mechanismwas to design a DiceWithStatistics class/object to have a Dice component class/object. This alsomayhave consequencies on later stages of the development DISCUSSION: What are the possible consequences (positive and negative)? CLUE: You may have seenthemwhenweconsidered the differentviews OBSERVERS: Wemaywish to createdifferentviewsthatautomatically update when the state of the models changes TSP: Object OrientedDevelopment

More Related