Objects First With Java

Download Report

Transcript Objects First With Java

Design: Coupling and Cohesion
How to write classes in a way that
they are easily understandable,
maintainable and reusable
Main concepts to be covered
•
•
•
•
Responsibility-driven design
Coupling
Cohesion
Refactoring
• Enumerations
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
2
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
3
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
4
World of Zuul
Chapter 7: zuul-bad
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
5
An Example
• Add two new directions to the World of Zuul:
• up
• down
• What do you need to change to do this?
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
6
Code Quality
• Two important concepts for quality of
code:
• Coupling – as little as possible please
• Cohesion – as much as possible please
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
7
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.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
8
Loose Coupling
• Loose coupling makes it possible to:
• understand how to use a class without having
to understand how that class works;
• change the implementation of a class without
affecting those classes that use it.
This improves maintainability … makes change easier.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
9
Cohesion
• Cohesion relates to: the the number and
diversity of tasks for which a single unit is
responsible.
• If a programming unit is responsible for
one logical task, we say it has high
cohesion.
• Cohesion applies both at the level of
classes and of methods.
• We aim for high cohesion.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
10
High Cohesion
• High cohesion makes it easier to:
• understand what a class or method does
(because it only does one thing);
• Choose and use descriptive names;
• reuse classes or methods.
This improves usability … and makes change easier.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
11
Cohesion of Classes
Classes should represent just one,
and only one, well defined entity.
Cohesion of Methods
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
12
Code Duplication
This is an indicator of bad design.
It makes maintenance harder.
It can lead to introduction of errors,
especially during maintenance.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
13
Add up
and down
exits?
World of Zuul
Chapter 7: zuul-bad
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad
Problems
• Fields in Room are all public.
• The fact that a Room has four exits is
burnt into the interface of its setExits
method.
• The Game class references Room fields.
• So, the implementation of the Game and
Room classes are coupled. A change in
one (e.g. adding more exits to a Room)
means changing the other (e.g. the
createRooms, printWelcome and
goRoom methods of Game).
• This is BAD!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad
Problems
• There are duplicated code fragments in
the Game class (in createRooms and
printWelcome methods).
• Changing the details of one of these
fragments means changing all.
• This is BAD!
• Factorise the duplicated code into a
single method (see Code 7.2, p209, of the
book: printLocationInfo) and
duplicate its invocation instead.
• Now, there is only one fragment of code
to maintain. This is Good!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad

Chapter 7: zuul-better
• There are duplicated code fragments in
the Game class (in createRooms and
printWelcome methods).
• Changing the details of one of these
fragments means changing all.
• This is BAD!
• Factorise the duplicated code into a
single method (see Code 7.2 of the book:
printLocationInfo) and duplicate its
invocation instead.
• Now, there is only one fragment of code
to maintain. This is Good!
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad

Chapter 7: zuul-better
• Make fields in Room private and provide accessor
methods to get them (e.g. getExit in Code 7.4).
• In Game, can no longer reference directly the exit
fields (all Rooms) of Room.
• In Game.printLocationInfo (which does so
reference), we could just change it to use the
accessor method, rather than the field.
• However, it’s neater to invoke a (new) method of
Room to get the exit information it needs to print
(Code 7.6). Each Room object has that
information, so it is really its responsibility.
• For Game.goRoom (which also does so reference),
thirteen lines of code collapse to one (see p214 in
“Fourth Edition”, but p201 in “Third Edition”).
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad

Chapter 7: zuul-better
• Make fields in Room private and provide accessor
methods to get them (e.g. getExit in Code 7.4).
• Look carefully at this getExit accessor …
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad

Chapter 7: zuul-better
public Room getExit (String direction)
{
if (direction.equals ("north")) {
return northExit;
}
if (direction.equals ("east")) {
return eastExit;
}
if (direction.equals ("south")) {
return southExit;
}
if (direction.equals ("west")) {
return westExit;
}
return null;
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Chapter 7: zuul-bad

Chapter 7: zuul-better
• Make fields in Room private and provide accessor
methods to get them (e.g. getExit in Code 7.4).
• In
Gamea, String
can no longer
reference
exit
It maps
(direction)
intodirectly
an exit the
Room
.
(all Room
s) of
Room.of a:
• fields
But that’s
just the
function
• In Game.printLocationInfo
(which does so
HashMap<String, Room>
reference), we could just change it to use the
• So,
replace
the four
exit than
fieldsthe
with
one of the
accessor
method,
rather
field.
(private
of course)
– Code
7.5. Now,
we of
• above
However,
it’s neater
to invoke
a (new)
method
can
have
exitsinformation
as we like.it needs to print
Room
to as
getmany
the exit
• Finally,
replace
theEach
setExits
method
(Code 7.6,
p218).
Room object
has(which
that
information,
so it is“four
reallyexits”
its responsibility.
burns the message
into its definition)
a single
setExit
method
(which
we can
• with
For Game
.goRoom
(which
also does
so reference),
thirteen
of code
collapse
to one (see p214).
invoke forlines
as many
exits
as we like).
• Now, Room is independent of its number of exits.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
World of Zuul
MORAL: class
diagrams are
not enough to
tell whether the
design is good.
Chapter 7: zuul-bad
Chapter 7: zuul-better
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
Responsibility-Driven Design
• Where should we add new fields and
new methods (and to which class)?
• The class that holds the data (fields)
processes (methods) the data.
• Responsibility-driven design leads to
low coupling.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
23
Localising Change
• One aim of reducing coupling and
responsibility-driven design is to
localise change.
• When a change is needed, as few
classes as possible should be
affected.
Improve maintainability … make change easier.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
24
Think Ahead
• When designing a class, think what
changes are likely to be made in the
future.
• Aim to make those changes easy.
Improve maintainability … make change easier.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
25
Refactoring
• When classes are maintained, code
usually needs to be added.
• Lists of fields, lists of methods and
the code inside methods tend to
become longer.
• 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
26
Refactoring and Testing
• When maintaining code, separate
the refactoring from making other
changes.
e.g. zuul-bad to zuul-better.
• Do the refactoring first, without
adding to the functionality.
• Test before and after refactoring to
ensure that nothing gets broken.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
27
Design Questions
• Common questions:
• How long should a class be?
• How long should a method be?
Answered with regard to cohesion and coupling.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
28
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 …
everything is still open to the designer.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
29
Enumerated Types
• A new language feature.
• Uses enum instead of class to introduce a new
type name and a set of named constants of that
type.
• This is a better (safer) alternative to a set of
named static int constants.
• For the World of Zuul, we shall use names for
command words, rather than string literals
(which will appear only in one place).
java.sun.com/docs/books/tutorial/java/javaOO/enum.html
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
30
A Basic Enumerated Type
public enum CommandWord
{
// A value for each command word,
// plus one for unrecognised commands.
GO, QUIT, HELP, UNKNOWN
}
Enumerated objects are constructed implicitly.
Each name represents an object of the
enumerated type, e.g. CommandWord.HELP.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
31
Equivalent Class Type
public class CommandWord
{
// A object for each command word,
// plus one for unrecognised commands.
public final static Commandword GO = new CommandWord ();
public final static Commandword QUIT = new CommandWord ();
public final static Commandword HELP = new CommandWord ();
public final static Commandword UNKNOWN = new CommandWord ();
private CommandWord ()
{
// nothing to do
}
}
The constructor is declared private so that no CommandWord
objects, other than the ones declared here, can be constructed.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
32
Another Enumerated Type
public enum CommandWord
{
// A value for each command word,
// plus one for unrecognised commands.
GO("go"), QUIT("quit"), HELP("help"), UNKNOWN("unknown");
private final String commandString;
private CommandWord (String commandString)
{
this.commandString = commandString;
}
public String toString ()
{
return commandString;
}
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
33
Equivalent Class Type
public class CommandWord
{
// A object for each command word,
// plus one for unrecognised commands.
public final static Commandword GO =
new CommandWord ("go");
public final static Commandword QUIT =
new CommandWord ("quit");
public final static Commandword HELP =
new CommandWord ("help");
public final static Commandword UNKNOWN =
new CommandWord ("?");
private final String commandString;
...
...
...
}
private constructor {to construct the above constants}
public String toString {returns the commandString}
public static CommandWord[] values {returns an array
of above constants}
Automatically provided by the enum
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
34
Equivalent Class Type
public class CommandWord
{
// A object for each command word,
// plus one for unrecognised commands.
public final static Commandword GO =
new CommandWord ("go");
public final static Commandword QUIT =
new CommandWord ("quit");
public final static Commandword HELP =
new CommandWord ("help");
public final static Commandword UNKNOWN =
new CommandWord ("?");
private final String commandString;
private CommandWord (String commandString) {
this.commandString = commandString;
}
...
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
35
Equivalent Class Type
public class CommandWord
{
...
public final statics (GO, QUIT, HELP, UNKNOWN)
private final String commandString;
private CommandWord (String commandString) {
this.commandString = commandString;
}
public String toString () {
return commandString;
}
Automatically provided by the enum
public static CommandWord[] values () {
return new CommandWord[] {GO, QUIT, HELP, UNKNOWN};
}
}
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
36
See also …
projects\chapter07\
courses\co320_ca13\projects-phw
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
37
Review
• Programs are continuously changed.
• It is important to anticipate change
and make it as easy as possible.
• Quality of code requires much more
than just performing correctly!
• Code must be understandable and
maintainable.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
38
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 maintain poorly structured
and well structured code.
• In fact, unless it is well structured, the code
is doomed … it will not be used for long.
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling
39