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.

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

Ch:8 Design Concepts S.W Design should have following quality attribute: Functionality Usability Reliability Performance Supportability (extensibility,
A Brief Introduction to Test- Driven Development Shawn M. Jones.
Comp1004: Building Better Classes II Software Design Partly based on BlueJ Book – Chapter 7.
Lesson-06 Designing classes
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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,
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
A Brief Introduction to Software Design and Design Quality By Laura Leventhal.
Designing Classes How to write classes in a way that they are easily understandable, maintainable and reusable.
Well-behaved objects Improving your coding skills 1.0.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Test-Driven Development “Test first, develop later!” –OCUnit.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Why Analysis Process Refer to earlier chapters Models what the system will do makes it easier for understanding no environment considered (hence, system.
Introduction to Object-oriented programming and software development Lecture 1.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
1 Chapter 5 Practice: A Generic View Software Engineering: A Practitioner’s Approach, 6th edition by Roger S. Pressman.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
4.1 Instance Variables, Constructors, and Methods.
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.
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Designing Classes 2 How to write classes in a way that they are easily understandable, maintainable and reusable.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Abstraction ADTs, Information Hiding and Encapsulation.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
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.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
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.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically assess the.
Class Diagrams Chapter 3. Classes and Objects Classes are the descriptions –definitions Objects are the things –instances.
Chapter 10 Software quality. This chapter discusses n Some important properties we want our system to have, specifically correctness and maintainability.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Objektorienterad programmering d2, förel. 8
CompSci 280 S Introduction to Software Development
Module Road Map Refactoring Why Refactoring? Examples
Chapter 5 Conclusion CIS 61.
Objects First with Java
Chapter 16 – Programming your App’s Memory
Objects First with Java
Overview of Eclipse Lectures
Advanced Java Programming
Class Diagrams – Part I Chapter 3.
Objektorienterad programmering d2, förel. 8
COS 260 DAY 14 Tony Gauvin.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CSG2H3 Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

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 bang on the keyboard for a while We want to think about what we are going to be writing first and then type

What should we be thinking about? There are 3 main things we should think about 1.Code Quality 2.Responsibility-driven design 3.Refactoring

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

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.

Code quality Two important concepts for quality of code: 1.Coupling 2.Cohesion These are related topics, but different ideas and sometime difficult to grasp. Code duplication is a third minor concept for code quality. If you have good coupling and cohesion, code duplication will be less of an issue.

Coupling Coupling describes the interconnectedness of classes We strive for loose coupling Each class should be largely independent of the other classes in the system Communication should be done through a well defined interface.

Cohesion Cohesion refers to how well a unit of code maps to a logical task. Each unit of code (method, class or module) is responsible for a well- defined task. There are two main types of cohesion –Method –Class

Method Cohesion Method cohesion refers to how well does a method do its job. In general, each method should do one thing and do it well. You can build up bigger methods from smaller ones.

Class Cohesion How well does a class do its job. Each class should focus on one idea and do it well. Classes should represent one noun from the project scope. For example, a CD class can be used to store tracks, as do real CDs You shouldn’t try to make a CD into a Frisbee. It’s not what it was designed to do and it doesn’t do it very well.

Code Duplication Code duplication is an indicator of bad design The problem is that any change to one piece of code requires changes to all places where that code is located It is easy to forget to change all the locations Worse yet, if you are working on someone else’s code, you may not know that the code duplication exists

Responsibility-Driven Design Responsibility-driven design expresses the idea that each class should be responsible for handling its own data. We can use this notion to determine which classes should implement a piece of functionality. If a class has the information necessary to process something, then it should do so. We can use this idea to decide where to put a method.

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.

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.

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.

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.

Making Extensions How well or poorly designed a class is, is sometimes very evident when you want to make extensions to a class. The question often comes up, how extensible is a class? A class that can be extended easily, was probably designed with care. A class that cannot be extended easily, may have been designed quickly, with little thought to how it might be extended.

Encapsulation Encapsulation is a major idea behind OOP Encapsulation suggests that only information about what a class can do should be visible to the outside How it does what it does should be hidden from the user of the object This is why we want to have our fields be private instead of public

Executing without BlueJ So far all we have done is write classes that can de run inside of BlueJ You might write a program one day that you want other people to be able to use In order to do that, you need to learn about class methods

Class Methods A class method is a method that can be run without an instance of the class Every Java program will have at least one class method, the main method public static void main( String[ ] Args )

Class Methods Any method that is declared to be static is a class method This will allow you to call a method without have an object around We saw one of these the other day: Math.abs() Also, System.out.println()

Limitations to Class Methods Since you don’t have an object around, static methods cannot use any variable that is not also declared to be static Also a static method may not call any other method that is not declared to be static.