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

Slides:



Advertisements
Similar presentations
Lilian Blot Announcements Teaching Evaluation Form week 9 practical session Formative Assessment week 10 during usual practical sessions group 1 Friday.
Advertisements

1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
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.
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.
Improving structure with inheritance
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
Objects First with Java A Practical Introduction using BlueJ
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Understanding class definitions Looking inside classes 3.0.
Well-behaved objects Improving your coding skills 1.0.
Information Hiding and Encapsulation
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.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
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.
OOP (Java) 7. Good Class Design Objectives
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Computer Science 240 © Ken Rodham 2006 Principles of Software Design.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
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.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
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.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
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.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
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.
Or how to work smarter when building solutions.  2:30 – 3:30 Mondays – focus on problem solving (with some terminology thrown in upon occasion)  All.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
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.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
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.
Objektorienterad programmering d2, förel. 8
DESIGNING CLASSES THE GAME OF LIFE
Module Road Map Refactoring Why Refactoring? Examples
Data Abstraction: The Walls
Objects First with Java
Objects First with Java
Chapter 6 Methods: A Deeper Look
Programming Logic and Design Fourth Edition, Comprehensive
COS 260 DAY 15 Tony Gauvin.
Collections and iterators
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
Presentation transcript:

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

2 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

3 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

4 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

5 world-of-zuul example Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling “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: or do a search for Colossal Cave Adventure.”

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

7 The Zuul Classes Game: Set up and loops to read/execute commands Parser: Reads user input and interprets commands Command: Checks user command for validity CommandWords: Defines all valid commands –stored in an array of String Room: Represents a location in the game –rooms can have exits leading to other rooms Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

9 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

10 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

11 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

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

Designing classes Coupling, cohesion, and responsibility-driven design

14 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

15 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

16 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

17 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?

18 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

19 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

20 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

21 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

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

23 Avoid code duplication for high cohesion Code duplication –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

24 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

25 Cohesion applied at different levels Class level: –each class should represent one single, well-defined entity (object) –with information (fields) pertaining to that class only Method level: –a method should be responsible for one and only one well-defined task –but may make calls to other methods to help perform other tasks Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

26 Cohesion for readability and reuse Readability: –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

27 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

28 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

29 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

30 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

31 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

32 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

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

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

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

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

37 Design questions Common questions: –How complex should a class be? –How long should a method be? These can now be answered in terms of cohesion and coupling Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

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

39 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

40 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

41 Class Methods So far, only used instance methods –invoked on an instance(object) of a class However, class methods are different –may be invoked WITHOUT a class object Similar to class variables in that the class methods BELONG to the class –having the class is enough to invoke it Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

42 Class Methods Defined by adding static keyword in front of the type name in the header public class Calendar { public static int getNumberOfDaysThisMonth() { … } … Such a method can then be called by specifying the class name int days = Calendar.getNumberOfDaysThisMonth(); There is NO object so the name of class is used before the dot Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling