1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

P5, M1, D1.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Comp1004: Building Better Classes II Software Design Partly based on BlueJ Book – Chapter 7.
Copyright by Scott GrissomCh 7 Designing Classes Slide 1 Well Designed Classes (Ch 7) Applications are a collection of interacting classes Our Goal It.
Lesson-06 Designing classes
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Improving structure with inheritance
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Objects First with Java A Practical Introduction using BlueJ
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Well-behaved objects Improving your coding skills 1.0.
Grouping Objects 1 Introduction to Collections.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
Introduction to Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically.
CO320 Introduction to Object- Oriented Programming Michael Kölling 3.0.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Lilian Blot Announcements The TPOP problem class this afternoon:  group 1 should come at 3.30pm and group 2 at 4pm. Teaching Evaluation Form  week 9.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
OOP (Java) 7. Good Class Design Objectives
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
5.0 Objects First with Java A Practical Introduction using BlueJ Introduction to Computer Science I Instructor: Allyson Anderson.
Program Design CMSC 201. Motivation We’ve talked a lot about certain ‘good habits’ we’d like you guys to get in while writing code. There are two main.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables Objects First with Java.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Cohesion and Coupling CS 4311
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
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.
1 COS 260 DAY 10 Tony Gauvin. 2 Agenda Questions? 4 th Mini quiz Today –Chapter 4 Assignment 2 Due Capstone Discussion Proposals Due Oct 15 No class on.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
1 COS 260 DAY 15 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz Today –Chapter 6 Assignment 4 posted –Due Nov 9 Capstone progress reports are due –Brief.
Objects First With Java A Practical Introduction Using BlueJ Designing classes How to write classes in a way that they are easily understandable, maintainable.
Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically assess the.
1 COS 260 DAY 17 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz –Chapter 7 –Password “GoBengals” –40 min Assignment 4 posted –Due Nov 9 (one week) Capstone.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Objektorienterad programmering d2, förel. 8
DESIGNING CLASSES THE GAME OF LIFE
CSC 222: Object-Oriented Programming Spring 2017
Objects First with Java A Practical Introduction using BlueJ
CSC 221: Computer Programming I Spring 2010
CSC 221: Computer Programming I Fall 2005
COS 260 DAY 17 Tony Gauvin.
Objects First with Java
Objects First with Java
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 15 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Presentation transcript:

1 COS 260 DAY 12 Tony Gauvin

2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If you are bored, Exercises 6.34, 6.35, 6.36 and 6.37 will be part of assignment 4 Capstone Proposals Over Due Discuss Designing Classes

Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0

4 Main concepts to be covered Responsibility-driven design Coupling Cohesion Refactoring Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

5 Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained, ported, adapted, … The work is done by different people over time (often decades). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

6 Change or die There are only two options for software: –Either it is continuously maintained –or it dies. Software that cannot be maintained will be thrown away. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 World of Zuul Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Explore zuul-bad

8 The Zuul Classes Game : The starting point and main control loop. Room: A room in the game. Parser: Reads user input. Command: A user command. CommandWords: Recognized user commands. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9 Code and design quality If we are to be critical of code quality, we need evaluation criteria. Two important concepts for assessing the quality of code are: –Coupling –Cohesion Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

10 Coupling Coupling refers to links between separate units of a program. If two classes depend closely on many details of each other, we say they are tightly coupled. We aim for loose coupling. A class diagram provides (limited) hints at the degree of coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11 Cohesion Cohesion refers to the number and diversity of tasks that a single unit is responsible for. If each unit is responsible for one single logical task, we say it has high cohesion. We aim for high cohesion. ‘Unit’ applies to classes, methods and modules (packages). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12 Code Duplication Examine Class Game for code duplications How can we fix the problem? Elimination of duplication fixes Bad Cohesion –One task per method –One entity per class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

13 An example to test quality Add two new directions to the 'World of Zuul': “up” “down” What do you need to change to do this? –Which classes How easy are the changes to apply thoroughly? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14 Up/down Extension Use of a HashMap for each room to store exits (directions and adjacent rooms ) –Key is a direction –Value is a (another) room exits.get(north) would return room that is north of the current room We then can add as many exist as want by add to the “exits” HashMap Would require changes to many classes (tight coupling) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Designing classes Coupling, cohesion, and responsibility-driven design

16 Coupling (reprise) Coupling refers to links between separate units of a program. If two classes depend closely on many details of each other, we say they are tightly coupled. We aim for loose coupling. A class diagram provides (limited) hints at the degree of coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 Loose coupling We aim for loose coupling. Loose coupling makes it possible to: –understand one class without reading others; –change one class with little or no effect on other classes. Thus: loose coupling increases maintainability. Encapsulation helps reduce coupling Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18 Tight coupling We try to avoid tight coupling. Changes to one class bring a cascade of changes to other classes. Classes are harder to understand in isolation. Flow of control between objects of different classes is complex. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

19 Cohesion (reprise) Cohesion refers to the number and diversity of tasks that a single unit is responsible for. If each unit is responsible for one single logical task, we say it has high cohesion. We aim for high cohesion. ‘Unit’ applies to classes, methods and modules (packages). Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

20 High cohesion We aim for high cohesion. High cohesion makes it easier to: –understand what a class or method does; –use descriptive names for variables, methods and classes; –reuse classes and methods. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 Loose cohesion We aim to avoid loosely cohesive classes and methods. Methods perform multiple tasks. Classes have no clear identity. Every part of class should refer to ONE thing Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22 Cohesion applied at different levels Class level: –Classes should represent one single, well defined entity. Method level: –A method should be responsible for one and only one well defined task. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23 Code duplication –is an indicator of bad design, –makes maintenance harder, –can lead to introduction of errors during maintenance. Very frustrating to Trouble Shoot to maintain code that has duplications. You can’t just fix one part you have find all the duplications Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

24 Responsibility-driven design Question: where should we add a new method (which class)? Each class should be responsible for manipulating its own data. The class that owns the data should be responsible for processing it. encapsulation! RDD leads to low coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

25 Localizing change One aim of reducing coupling and responsibility-driven design is to localize change. When a change is needed, as few classes as possible should be affected. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

26 Implicit coupling Occurs when one class depends on the internal information of another class Example add a new command to the game world-of-zuul –Add to CommandWord class –Modify Game class Add new method for command Add new command method to processCommand method logic What else? Objects First with Java - A Practica l Introduction using BlueJ, © David J. Barnes, Michael Kölling

27 Thinking ahead When designing a class, we try to think what changes are likely to be made in the future. We aim to make those changes easy. One pattern often seen Text based game becomes GUI based Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

28 Model View Controller Originates from Smalltalk (basis for most modern object oriented programming languages) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

29 Cohesion in Depth Cohesion of Methods –One method   one task Cohesion of Classes –One class  one well defined entity Cohesion for readability & reuse –Proper naming and class Division provides for easier to understand and reuse code. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

30 Refactoring When classes are maintained, often code is added. Classes and methods tend to become longer. (bloat code) Every now and then, classes and methods should be refactored to maintain cohesion and low coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

31 Refactoring and testing When refactoring code, separate the refactoring from making other changes. First do the refactoring only, without changing the functionality. Test before and after refactoring to ensure that nothing was broken. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

32 Design questions Common questions: –How long should a class be? –How long should a method be? These can now be answered in terms of cohesion and coupling. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

33 Design guidelines A method is too long if it does more then one logical task. A class is too complex if it represents more than one logical entity. Note: these are guidelines - they still leave much open to the designer. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

34 Enumerated Types A language feature. Uses enum instead of class to introduce a type name. Their simplest use is to define a set of significant names. –Alternative to static int constants. –When the constants’ values would be arbitrary. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

35 A basic enumerated type Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling public enum CommandWord { // A value for each command word, // plus one for unrecognised commands. GO, QUIT, HELP, UNKNOWN; } Each name represents an object of the enum type, e.g., CommandWord.HELP. Enum objects are not created directly. Enum definitions can also have fields, constructors and methods.

36 Review Programs are continuously changed. It is important to make this change possible. Quality of code requires much more than just performing correct at one time. Code must be understandable and maintainable. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

37 Review Good quality code avoids duplication, displays high cohesion, low coupling. Coding style (commenting, naming, layout, etc.) is also important. There is a big difference in the amount of work required to change poorly structured and well structured code. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling