Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0."— Presentation transcript:

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

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

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

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

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

6 6 World of Zuul (2) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 7 Code quality Two important concepts for quality of code: –Coupling –Cohesion

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

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

10 10 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Cohesion Cohesion refers to the 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. Cohesion applies to classes and methods. We aim for high cohesion.

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

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

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

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

15 15 Code duplication (2) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

16 16 Making Extension We want to add 2 new direction: –up –down Which class or classes should be change? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17 17 Making Extension (2) How does Exits information implemented? Is the implementation efficient? How can improve it? Encapsulation: –Hiding implementation information from view Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18 18 Making Extension (3) Public or private? Using accessor method Loose the coupling Public parts of a class are parts of the interface. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

19 19 Applying Encapsulation (1) private String description; private Room northExit; private Room southExit; private Room eastExit; private Room westExit; 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

20 20 Applying Encapsulation (2) Instead of writing nextRoom = currentRoom.eastExit; We now write: nextRoom = currentRoom.getExit("east"); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21 21 Applying Encapsulation (3) Room nextRoom = null; if(direction.equals("north")) nextRoom = currentRoom.getExit("north"); if(direction.equals("east")) nextRoom = currentRoom.getExit("east"); if(direction.equals("south")) nextRoom = currentRoom.getExit("south"); if(direction.equals("west")) nextRoom = currentRoom.getExit("west"); Room nextRoom = currentRoom.getExit(direction); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

22 22 Changing Implementation (1) Using Map for storing neighbors information. private HashMap exits; Write setExit and getExit. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23 23 Changing Implementation (2) public void setExit(String direction, Room neighbor) { exits.put(direction, neighbor); } lab.setExits(outside, office, null, null); lab.setExit("north", outside); lab.setExit("east", office); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

25 25 Responsibility-driven design (2) Which class is responsible to make exits info? Which class is responsible to make description of a room? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

26 26 Applying RDD (1) public String getExitString() { String exitString = "Exits: "; if(northExit != null) exitString += "north "; if(eastExit != null) exitString += "east "; if(southExit != null) exitString += "south "; if(westExit != null) exitString += "west "; return exitString; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

27 27 Applying RDD (2) public String getExitString() { String returnString = "Exits:"; Set keys = exits.keySet(); for(String exit : keys) { returnString += " " + exit; } return returnString; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

28 28 Further RDD to Room Class In Game Class: System.out.println("You are " + currentRoom.getDescription()); System.out.println(currentRoom.getExitString()); items If later we add other items to Room, we must change the code above. Sloution: Adding new method that return full detail of the Room. (getLongDescription) ^ 6.12, 6.13 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

29 29 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.

30 30 Implicit coupling One class is depends on internal information of another class. New command: look Adding look to valid commands and implement it. And now issuing the help command, new command does not list! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

31 31 Implicit coupling (2) Who must list the valid commands? Zuul-better Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling GameParser CommandWords

32 32 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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. For example adding GUI to Zuul …

33 33 Cohesion (again!) Method cohesion –Print welcome Class cohesion –Another extension to zuul: Adding item to game –Which class must describe the items? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

34 34 Cohesion (2) Cohesion for –Readability –Reuse  7.23~7.26 (Stack) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

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

37 37 Refactoring and testing (2) Adding item to game. The player can carry some items. The items and their weight must be keep somewhere. We need a field to keep the maximum weight the player can carry. Where should we add these data? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

38 38 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Design guidelines Design hint: –Don’t put too much into a single method. –Don’t put everything into one class. Common questions: –How long should a class be? –How long should a method be? Can now be answered in terms of cohesion and coupling.

39 39 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Design guidelines (2) A method is too long if it does more than 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.

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

41 41 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Enumerated Types (2) 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 enumerated type, e.g. CommandWord.HELP. Enumerated objects are not created directly by the programmer.

42 42 Enumerated Types (3) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Example: public enum Size { SMALL, MEDIUM, LARGE }; Typical use: Size imageSize = Size.MEDIUM; If (imageSize == Size.SMALL)... Safer than integer constants public static final int SMALL = 1; public static final int MEDIUM = 2; public static final int LARGE = 3;

43 43 Enumerated Types (extra!) enum equivalent to class with fixed number of instances public class Size { private /* ! */ Size() { } public static final Size SMALL = new Size(); public static final Size MEDIUM = new Size(); public static final Size LARGE = new Size(); } enum types are classes; can add methods, fields, constructors

44 44 Class Methods Instance method Class method Static fields Static methods pubic static void main(…) Limitations of class methods Math class Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

45 45 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 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. the nonfunctional aspects of an application.

46 46 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 important. There is a big difference in the amount of work required to change poorly structured and well structured code.


Download ppt "Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0."

Similar presentations


Ads by Google