Objektorienterad programmering d2, förel. 8

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

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
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
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,
Design The goal is to design a modular solution, using the techniques of: Decomposition Abstraction Encapsulation In Object Oriented Programming this is.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
1 / 31 CS 425/625 Software Engineering User Interface Design Based on Chapter 15 of the textbook [SE-6] Ian Sommerville, Software Engineering, 6 th Ed.,
Jump to first page 1 System Design (Finalizing Design Specifications) Chapter 3d.
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.
Object-Orientated Design Unit 3: Objects and Classes Jin Sa.
Lilian Blot Announcements The TPOP problem class this afternoon:  group 1 should come at 3.30pm and group 2 at 4pm. Teaching Evaluation Form  week 9.
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.
Systems Analysis – Analyzing Requirements.  Analyzing requirement stage identifies user information needs and new systems requirements  IS dev team.
Chapter 9: Coupling & Cohesion Omar Meqdadi SE 273 Lecture 9 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Cohesion and Coupling CS 4311
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
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.
Software Engineering Principles. SE Principles Principles are statements describing desirable properties of the product and process.
Chapter 10 Software Engineering. Understand the software life cycle. Describe the development process models. Understand the concept of modularity in.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
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.
Systems Design.  Application Design  User Interface Design  Database Design.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Implementation Topics Describe –Characteristics of good implementations –Best practices to achieve them Understand role of comments Learn debugging techniques.
9 Improving structure with inheritance
Objektorienterad programmering d2, förel. 8
DESIGNING CLASSES THE GAME OF LIFE
Coupling and Cohesion Rajni Bhalla.
Introduction Edited by Enas Naffar using the following textbooks: - A concise introduction to Software Engineering - Software Engineering for students-
Coupling and Cohesion 1.
System Design Ashima Wadhwa.
Conception OBJET GRASP Patterns
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
Lecture 2 Introduction to Programming
Objects First with Java
Introduction Edited by Enas Naffar using the following textbooks: - A concise introduction to Software Engineering - Software Engineering for students-
Objects First with Java
Unit 6 Assignment 2 Chris Boardley.
Programming Logic and Design Fourth Edition, Comprehensive
COS 260 DAY 15 Tony Gauvin.
Software Design Lecture : 8
CSE 403, Winter 2003 Software Engineering
COS 260 DAY 14 Tony Gauvin.
A Level Computer Science Exam Technique
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Object-Oriented PHP (1)
Use Case Analysis – continued
Chapter 9: Implementation
Presentation transcript:

Objektorienterad programmering d2, förel. 8 8 Designing classes BK Chap. 8 How to write classes that are easily understandable, maintainable and reusable DAT050, DAI2, 16/17, lp 1

Main concepts to be covered Objektorienterad programmering d2, förel. 8 Main concepts to be covered Responsibility-driven design Coupling Cohesion Refactoring Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 World of Zuul Explore zuul-bad Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 The Zuul Classes Game: The starting point and main control loop Room: A room in the game Parser: Reads user input Command: A user command CommandWords: Recognized user commands Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

An example to test quality Objektorienterad programmering d2, förel. 8 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? Låt varje rum innehålla ett föremål med beskrivning och vikt Är det bra design att införa attribut för detta i Room? TÄNK FRAMÅT! - Kan föremålet bäras iväg? - Kan man ha flera föremål? Inför klassen Item! Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 Kör resten av bilderna - Sen DEMO DAT050, DAI2, 16/17, lp 1

Code and design quality Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 (limited) hints at the degree of coupling Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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) Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Cohesion ”on the road” Which brake system design has the highest cohesion? Återvänd till detta efter sista bilden (före demon). I fälgbromsen är fälgen den passiva delen i bromsen. Pga denna design måste hjulet bytas ut vid slitage. Hjulet har fått en extra roll som komponent i ett annat system (bromssystemet). Skivbromsen är designad som ett eget väl sammanhållet system. Alla förslitningsdetaljer hör till själva bromsen. Slitage i bromsen påverkar inga andra komponenter. Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Loose cohesion We aim to avoid loosely cohesive classes and methods: Methods performing multiple tasks Classes having no clear identity Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Summary Coupling Cohesion Tight/High   Loose/Low Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Cohesion applied at different levels Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Code duplication Code duplication is an indicator of bad design makes maintenance harder can lead to introduction of errors during maintenance Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Responsibility-Driven Design Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 high cohesion and low coupling Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Refactoring and testing Objektorienterad programmering d2, förel. 8 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 Refactor Test! Change Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Design questions Common questions: How long should a class be? How long should a method be? These can be answered in terms of cohesion and coupling Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Enumerated Types BK 6.13.1 A language feature Uses enum instead of class to introduce a type name A common use is to define a set of significant names Alternative to static int constants (When the constants’ values would be arbitrary.) Se BK 7.13 ! Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

A basic enumerated type Objektorienterad programmering d2, förel. 8 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 enumerated type, e.g. CommandWord.HELP Enum objects are not created directly by the programmer Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1

Objektorienterad programmering d2, förel. 8 Review Good quality code avoids duplication, displays high cohesion and 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 Objektorienterad programmering, DAT050, DAI2, 16/17, lp 1 DAT050, DAI2, 16/17, lp 1