Objects First with Java

Slides:



Advertisements
Similar presentations
Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.
Advertisements

Comp1004: Building Better Classes II Software Design Partly based on BlueJ Book – Chapter 7.
Lesson-06 Designing classes
Inheritance Inheritance Reserved word protected Reserved word super
Software Engineering and Design Principles Chapter 1.
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,
© Copyright 2011 John Wiley & Sons, Inc.
Module: Definition ● A logical collection of related program entities ● Not necessarily a physical concept, e.g., file, function, class, package ● Often.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 1 Software Development. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 1-2 Chapter Objectives Discuss the goals of software development.
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.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
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.
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.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Object Oriented Analysis and Design Introduction.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Computer Science 240 © Ken Rodham 2006 Principles of Software Design.
SE: CHAPTER 7 Writing The Program
Cohesion and Coupling CS 4311
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Software Engineering Principles. SE Principles Principles are statements describing desirable properties of the product and process.
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.
Chapter 10 Software quality. This chapter discusses n Some important properties we want our system to have, specifically correctness and maintainability.
CSCE 240 – Intro to Software Engineering Lecture 3.
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.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
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
Visit for more Learning Resources
SAT Prep Lesson # 1 EQ: What do I need know about time management to be successful on the SAT?
Classroom Assessment A Practical Guide for Educators by Craig A
Objects First with Java A Practical Introduction using BlueJ
Coupling and Cohesion 1.
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Data Abstraction: The Walls
Systems Analysis and Design
COS 260 DAY 17 Tony Gauvin.
Objects First with Java
Objects First with Java A Practical Introduction using BlueJ
Object Based Programming
Chapter 9 Object-Oriented Programming: Inheritance
Improving the Design “Can the design be better?”
Chapter 6 Methods: A Deeper Look
COS 260 DAY 18 Tony Gauvin.
Java Programming Arrays
Programming Logic and Design Fourth Edition, Comprehensive
COS 260 DAY 15 Tony Gauvin.
Software Design Lecture : 9.
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
COS 260 DAY 23 Tony Gauvin.
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
Object-Oriented PHP (1)
S. Single Responsibility Principle O. L. I. D.
C++ Object Oriented 1.
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 © 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, … 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.

Code quality It is possible to implement an application and to get it to perform its task with badly designed classes. Just executing a finished application does not usually indicate whether it is structured well internally or not. The problems typically surface when a maintenance programmer wants to make some changes to an existing application. If, for example, a programmer attempts to fix a bug, or wants to add new functionality to the program, a task that might be easy and obvious with well designed classes may well be very hard and involve a great deal of work if the classes are badly designed.

Code and design quality Objects First with Java 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 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java 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 hints at where coupling exists. The degree of coupling determines how hard it is to make changes in an application. In a tightly coupled class structure, a change in one class can make it necessary to change several other classes as well. This is what we try to avoid, because the effect of making one small change can quickly ripple through a complete application. In addition, finding all the places where changes are necessary and actually making the changes can be difficult and time consuming. In a loosely coupled system, on the other hand, we can often make changes to one class without making any changes to other classes, and the application will still work. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Objects First with Java 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). Ideally, one unit of code should be responsible for one cohesive task (that is, one task that can be seen as a logical unit). The main reason behind the principle of cohesion is reuse: if a method or class is responsible for only one well-defined thing, then it is much more likely that it can be used again in a different context. A complementary advantage of following this principle is that, when change is required to some aspect of an application, we are likely to find all the relevant pieces located in the same unit. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and 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. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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.

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

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

Implicit coupling Linkage not necessarily expressed via data and method interactions. Can arise from assumptions about the way a class is implementing. Satisfactory resolution might require a small increase in explicit coupling. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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

Objects First with Java 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. The main reason behind the principle of cohesion is reuse: if a method or class is responsible for only one well-defined thing, then it is much more likely that it can be used again in a different context. A complementary advantage of following this principle is that, when change is required to some aspect of an application, we are likely to find all the relevant pieces located in the same unit. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Loose cohesion We aim to avoid loosely cohesive classes and methods: Methods performing multiple tasks. Classes modeling multiple entities. Classes having no clear identity. Modules/Packages of unrelated classes. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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.

Objects First with Java Code duplication An indicator of bad design. Makes maintenance harder. Can lead to the introduction of inconsistencies and errors during maintenance/modification. Occurs at both method and class level. The problem with code duplication is that any change to one version must also be made to the other if we are to avoid inconsistency. This increases the amount of work a maintenance programmer has to do, and it introduces the danger of bugs. It happens very easily that a maintenance programmer finds one copy of the code and, having changed it, assumes that the job is done. There is nothing indicating that a second copy of the code exists, and it might incorrectly remain unchanged. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. © David J. Barnes and Michael Kölling

Thinking ahead When designing a class, we try to think about changes likely to be made in the future. We aim to make those changes easy. Requires a little more effort now to greatly reduce effort later. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Refactoring When classes are maintained code is usually added. Classes and methods tend to become longer. Every now and then, classes and methods should be refactored to maintain cohesion and low coupling. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

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

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

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

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

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