Designing classes

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

3.0

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

2

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

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

A Practical Introduction using BlueJ.World of Zuul Objects First with Java . © David J. Michael Kölling 5 . Barnes.

Code quality ‡ Two important concepts for quality of code: ² Coupling ² Cohesion Objects First with Java .A Practical Introduction using BlueJ. © David J. Barnes. Michael Kölling 6 .

Objects First with Java . Barnes. ‡ If two classes depend closely on many details of each other. © David J. ‡ We aim for loose coupling. we say they are tightly coupled. Michael Kölling 7 .A Practical Introduction using BlueJ.Coupling ‡ Coupling refers to links between separate units of a program.

Barnes. ² Thus: improves maintainability.A Practical Introduction using BlueJ. © David J. Objects First with Java .Loose coupling ‡ Loose coupling makes it possible to: ² understand one class without reading others. Michael Kölling 8 . ² change one class without affecting others.

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

Objects First with Java . Michael Kölling 10 . Barnes. ² reuse classes or methods. ² use descriptive names.High cohesion ‡ High cohesion makes it easier to: ² understand what a class or method does. © David J.A Practical Introduction using BlueJ.

Cohesion of methods ‡ A method should be responsible for one and only one well defined task. Barnes. Objects First with Java . © David J.A Practical Introduction using BlueJ. Michael Kölling 11 .

A Practical Introduction using BlueJ. Michael Kölling 12 . Barnes. © David J. well defined entity. Objects First with Java .Cohesion of classes ‡ Classes should represent one single.

Objects First with Java .A Practical Introduction using BlueJ. © David J. Barnes. Michael Kölling 13 . ² can lead to introduction of errors during maintenance. ² makes maintenance harder.Code duplication ‡ Code duplication ² is an indicator of bad design.

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

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

Thinking ahead ‡ When designing a class. Objects First with Java . Barnes. Michael Kölling 16 . © David J.A Practical Introduction using BlueJ. ‡ We aim to make those changes easy. we try to think what changes are likely to be made in the future.

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

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

A Practical Introduction using BlueJ. Barnes. © David J. Michael Kölling 19 .Design questions ‡ Common questions: ² How long should a class be? ² How long should a method be? ‡ Can now be answered in terms of cohesion and coupling. Objects First with Java .

they still leave much open to the designer. Objects First with Java . Michael Kölling 20 . © David J.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.A Practical Introduction using BlueJ. ‡ Note: these are guidelines . Barnes.

Enumerated Types ‡ A language feature. ‡ Simplest use is to define a set of significant names. Barnes. ² Alternative to static int constants. ‡ Uses enum instead of class to introduce a type name. © David J. Objects First with Java . Michael Kölling 21 .A Practical Introduction using BlueJ.

HELP. CommandWord. QUIT.HELP. } ‡ Each name represents an object of the enumerated type. UNKNOWN. GO.A basic enumerated type public enum CommandWord { // A value for each command word. // plus one for unrecognised commands.A Practical Introduction using BlueJ. © David J. Barnes. Michael Kölling 22 . e. Objects First with Java .g. ‡ Enumerated objects are not created directly by the programmer.

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

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

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master Your Semester with a Special Offer from Scribd & The New York Times

Cancel anytime.