Objects First with Java

Slides:



Advertisements
Similar presentations
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation For C# Developers.
Advertisements

Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
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
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Module: Definition ● A logical collection of related program entities ● Not necessarily a physical concept, e.g., file, function, class, package ● Often.
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.
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.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
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.
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.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
SE: CHAPTER 7 Writing The Program
Chapter 7 Object-Oriented Design Concepts
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
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 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.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Objects First With Java A Practical Introduction Using BlueJ Designing classes How to write classes in a way that they are easily understandable, maintainable.
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.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
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.
Principles of Programming & Software Engineering
CSC 222: Object-Oriented Programming
for-each PROS CONS easy to use access to ALL items one-by-one
Objektorienterad programmering d2, förel. 8
DESIGNING CLASSES THE GAME OF LIFE
CSC 222: Object-Oriented Programming Spring 2017
Module Road Map Refactoring Why Refactoring? Examples
Objects First with Java A Practical Introduction using BlueJ
Objects First with Java Introduction to collections
Coupling and Cohesion 1.
CSC 221: Computer Programming I Spring 2010
CSC 221: Computer Programming I Fall 2005
Data Abstraction: The Walls
COS 260 DAY 17 Tony Gauvin.
Understanding class definitions
Object Based Programming
Objects First with Java
Overview of Eclipse Lectures
Chapter 6 Methods: A Deeper Look
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
Programming Logic and Design Fourth Edition, Comprehensive
COS 260 DAY 15 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
Presentation transcript:

Objects First with Java Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0 © David J. Barnes and Michael Kölling

Main concepts to be covered Responsibility-driven design Coupling Cohesion Refactoring Encapsulation © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Software changes Software is not like a novel that is written once and then remains unchanged Software is extended, corrected, maintained, ported, adapted, etc… The work is done by different people over time (often decades) © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

world-of-zuul example “Our world-of-zuul game is modeled on the original Adventure game that was developed in the early 1970s by Will Crowther and expanded by Don Woods. The original game is also sometimes known as the Colossal Cave Adventure. This was a wonderfully imaginative and sophisticated game for its time, involving finding your way through a complex cave system, locating hidden treasure, using secret words, and other mysteries, all in an effort to score the maximum number of points. You can read more about it at sites such as: http://jerz.setonhill.edu/if/canon/Adventure.htm http://www.rickadams.org/adventure/ or do a search for Colossal Cave Adventure.” Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

World of Zuul Explore zuul-bad © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Code and design quality Criteria needed to define how to evaluate code quality Two important concepts for assessing the quality of code are: Coupling Cohesion © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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 However, we aim for loose coupling where classes are not so inter-connected 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

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 responsible for only one cohesive task A unit applies to classes, methods and modules (packages) for reusability and maintainability Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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? How easy are the changes to apply thoroughly? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Finding relevant source code What do we change to add 2 new directions? Class Room exits of each room stored as fields exits assigned in setExits method Class Game exit info printed in printWelcome method exits defined in createRoom method exits used in goRoom to find next room Where and how easy is it to apply? Must add up and down options to ALL of these places … making it VERY difficult.

Coupling in zuul Coupling refers to links between units of a program so many places where all exits are enumerated If two classes depend closely on many details of each other, we say they are tightly coupled detail change in one requires change in ALL the others Aim for loose coupling where changes affect the implementation (how) and not the interface (what) 1 class implementation change should not change others A class diagram provides (limited) hints at the degree of coupling fewer arrow(-->) == less dependencies == encapsulation

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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

Encapsulation to reduce coupling public class Room { public String description; public Room northExit; public Room southExit; public Room eastExit; public Room westExit; ... What is wrong with the fields of this class Room?

Encapsulation to reduce coupling public class Room { public String description; public Room northExit; public Room southExit; public Room eastExit; public Room westExit; ... What is wrong with the fields of this class Room? Fields are declared as public!! allows direct access from ANY other class exposes how exit information is stored no longer hides implementation from view breaks encapsulation guideline suggesting only what a class does is visible to the outside

Reducing coupling Encapsulation supports loose coupling private elements cannot be referenced from outside the class Reduces the impact of internal changes Responsibility-driven design supports loose coupling Driven by data location Enhanced by encapsulation © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Cohesion in zuul Cohesion refers to the number and diversity of tasks that a single unit is responsible for many methods print the current room details in addition to performing another separate task If a unit performs multiple tasks, we say it has loose cohesion each method then has no clear responsibility Aim for high cohesion where each unit is responsible for only one single logical task each method should only have implementation for 1 task Unit applies to classes, methods and modules single tasks == modular units == less code duplication Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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 Allows for readability and reuse Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

Cohesion for readability and reuse makes a class more readable easier for maintenance programmer to recognize where to make changes Reuse: separate methods into single tasks creates higher potential of being reused in many different places Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Code duplication Both the printWelcome & goRoom methods contain the following lines of code to print the current room details: System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) { System.out.print("north "); } if(currentRoom.eastExit != null) { System.out.print("east "); if(currentRoom.southExit != null) { System.out.print("south "); if(currentRoom.westExit != null) { System.out.print("west "); System.out.println(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Avoid code duplication for high cohesion is an indicator of bad design makes maintenance harder increases chance of inconsistencies leads to errors during maintenance not all copies of code are changed loose cohesion with parts of multiple method doing the same thing separate into more cohesive units Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

printLocationInfo( ) private void printLocationInfo() { System.out.println("You are " + currentRoom.getDescription()); System.out.print("Exits: "); if(currentRoom.northExit != null) { System.out.print("north "); } if(currentRoom.eastExit != null) { System.out.print("east "); if(currentRoom.southExit != null) { System.out.print("south "); if(currentRoom.westExit != null) { System.out.print("west "); System.out.println(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Responsibility-driven design 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 RDD leads to low coupling Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

Thinking ahead When designing a class, try to think what changes are likely to be made in the future We aim to make those changes easy Suppose an existing program is upgraded from a text interface to graphical: replace ALL System.out.println statements too many hard-coded instances to change better to encapsulate all information about the user interface in a single class … at the start then other classes should produce information to pass to the “user interface” class to present so changes to the user interface would be localized to only 1 class … the “user interface” Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Refactoring When classes are maintained or changed, often code is added Classes and methods tend to become longer, possibly losing high cohesion and loose coupling Every now and then, classes and methods should be refactored to maintain its high cohesion and low coupling Refactoring means rethinking and redesigning the program’s class and method structures Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Refactoring and testing HOWEVER ... 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 Then, continue with maintenance or changes on Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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 Module/Package level: Groups of related classes © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Design guidelines How complex should a class be? A class is too complex if it represents more than one logical entity How long should a method be? A method is too long if it does more then one logical task Note: these are just guidelines - they still leave much open to the designer © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Enumerated Types A language feature defining a type Declared like a class using enum instead of class to introduce a type name Used to define a list of variable names denoting the set of values belonging to this type: 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

A basic enumerated type public enum CommandWord { GO, QUIT, HELP, UNKNOWN } By convention, names are defined in CAPS 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Using enumerated types public enum CommandWord { GO, QUIT, HELP, UNKNOWN } String commandWord = command.getCommandWord(); if(commandWord.equals("help")) { printHelp(); } else if(commandWord.equals("go")) { goRoom(command); else if(commandWord.equals("quit")) { wantToQuit = quit(command); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

String type commandWord CommandWord type commandWord if(commandWord.equals("help")) { printHelp(); } else if(commandWord.equals("go")) { goRoom(command); else if(commandWord.equals("quit")) { wantToQuit = quit(command); String type commandWord public enum CommandWord { GO, QUIT, HELP, UNKNOWN } data type using enum CHANGE if(commandWord == CommandWord.HELP) { printHelp(); } else if(commandWord == CommandWord.GO) { goRoom(command); else if(commandWord == CommandWord.QUIT) { wantToQuit = quit(command); CommandWord type commandWord

Use switch to express code intent even more clearly ... if(commandWord == CommandWord.HELP) { printHelp(); } else if(commandWord == CommandWord.GO) { goRoom(command); else if(commandWord == CommandWord.QUIT) { wantToQuit = quit(command); BEST Use switch to express code intent even more clearly ... switch (commandWord) { case HELP: printHelp(); break; case GO: goRoom(command); case QUIT: wantToQuit = quit(command); }

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

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