nouns verbs methods class noun

Similar documents
D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

More on Objects and Classes

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

D06 PROGRAMMING with JAVA

Chapter 13 - Inheritance

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Iteration CHAPTER 6. Topic Summary

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

An Introduction to Classes

ICOM 4015: Advanced Programming

UML Class Diagrams. Lesson Objectives

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Computing Concepts with Java Essentials

Inheritance, overloading and overriding

Unit Testing & JUnit

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Chapter 2: Elements of Java

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Fundamentals of Java Programming

Session 7 Fractions and Decimals

Moving from CS 61A Scheme to CS 61B Java

Chapter 3. Input and output. 3.1 The System class

AP Computer Science Static Methods, Strings, User Input

OO Design Quality Metrics

x 2 + y 2 = 1 y 1 = x 2 + 2x y = x 2 + 2x + 1

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

ATM Case Study Part 1

So we thought we knew money

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

LAB4 Making Classes and Objects

11 November

Understanding class definitions

Java Interview Questions and Answers

CS193j, Stanford Handout #10 OOP 3

Some Scanner Class Methods

Lecture 5: Java Fundamentals III

Java Programming Fundamentals

D06 PROGRAMMING with JAVA

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

COMP 110 Prasun Dewan 1

Queuing Theory. Long Term Averages. Assumptions. Interesting Values. Queuing Model

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Java: Learning to Program with Robots. Chapter 11: Building Quality Software

Software Engineering Techniques

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

How to Make a Domain Model. Tutorial

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Object Oriented Software Design

Abstract Classes. Suppose we want write a program that manipulates various types of bank accounts. An Account typically has following features;

Days. Day 1. Reflection Teacher Responsibilities. Lesson Plans

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

ECE 333: Introduction to Communication Networks Fall 2002

Approach of Unit testing with the help of JUnit

Java applets. SwIG Jing He

Building Java Programs

Minimax Strategies. Minimax Strategies. Zero Sum Games. Why Zero Sum Games? An Example. An Example

Computer Programming I

Chapter 8 Approaches to System Development

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Object Oriented Software Design

Experiences with Online Programming Examinations

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

1 File Processing Systems

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Material and some slide content from: - Software Architecture: Foundations, Theory, and Practice NFPs Reid Holmes Lecture 5 - Tuesday, Sept

Introduction to Programming

Kant s deontological ethics

Limitations of Data Encapsulation and Abstract Data Types

Perimeter, Area and Volume What Do Units Tell You About What Is Being Measured? Overview

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Introducing Formal Methods. Software Engineering and Formal Methods

CS 111 Classes I 1. Software Organization View to this point:

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

C# and Other Languages

Course Title: Software Development

CS 106 Introduction to Computer Science I

Introduction to Object-Oriented Programming

Building Java Programs

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

We will study the extreme case of perfect competition, where firms are price takers.

Java Basics: Data Types, Variables, and Loops

C H A P T E R Regular Expressions regular expression

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

New Generation of Software Development

(Refer Slide Time 00:56)

Teaching Pre-Algebra in PowerPoint

Characteristics of Binomial Distributions

Transcription:

COMP209 Object Oriented Programming Designing Classes Mark Hall Overview Choosing Classes Cohesion & Overview Designing classes can be a challenge How to start? Is the result of good quality? Good class design involves understanding and utilizing a number of software design best practices Strive for classes that are modular, reusable and bug-free A good class abstracts away implementation detail behind a simple and intuitive interface Has low coupling, high cohesion and minimal side-effects Apply solutions described in design patterns These are good OO techniques applied successfully to different types of problems by others Department of Computer Science 1 Department of Computer Science 2 If you have experience with another programming language (other than Java), you may be used to programming functions A function carries out an action In OO programming, the actions appear as methods Each method, however, belongs in a class Classes are blueprints for objects Objects are not actions they are entities Identify objects and the classes to which they belong Rule of thumb: Class names should be nouns, and method names should be verbs What makes a good class? A class should represent a single concept Eg. Concepts from mathematics: Point, PlanarPoint, Rectangle, Eclipse Other classes are abstractions of real-life entities BankAccount, LibraryBook, Customer Department of Computer Science 3 Department of Computer Science 4 For these classes, the properties of a typical object are easy to understand A Rectangle object has width and height Given a BankAccount object (see the example code from last lecture) you can deposit and withdraw money Generally, concepts from the part of the universe that our program concerns make good classes The name for such a class should be a noun that describes the concept Another useful category of classes are those that do some kind of work for you A StreamTokenizer object breaks up an input stream into individual tokens A Random object (from the java.util package) generates random numbers It is a good idea to choose class names for these types of classes that end in -er or -or A better name for Random might be RandomNumberGenerator Department of Computer Science 5 Department of Computer Science 6 Department of Computer Science 1

Very occasionally, a class has no objects, but it contains a collection of related static methods and constants Eg java.lang.math: static double PI static double E static double abs(double a) static double cos(double a) Etc. Such a class is called a utility class Department of Computer Science 7 What might not be a good class? If you can t tell from the class name what an object of the class is supposed to do Eg. Say you had an assignment to write a program that prints pay-cheques You might decide to design a PaychequeProgram class What would an object of this class do? Everything that the assignment requires! To simplify things, it would be better to have a Paycheque class. Then you program could manipulate Paycheque objects The Paycheque class might also be able to be re-used in another application Department of Computer Science 8 What might not be a good class? Turning a function into a class Eg. A ComputePaycheque class Can you visualize a ComputePaycheque object? ComputePayCheque isn t a noun On the other hand, a Paycheque object makes intuitive sense The word paycheque is a noun You can visualize a paycheque object You can think about useful methods of the Paycheque class, such as compute Department of Computer Science 9 Cohesion and Cohesion and coupling are useful criteria for analyzing the quality of the public interface of a class Cohesion: A class should represent a single concept The public methods and constants exposed by the interface should be cohesive ie. all interface features should be closely related to the single concept that the class represents If the public interface to a class refers to multiple concepts, then it may be time to use separate classes instead Department of Computer Science 10 Cohesion Consider the public interface for a (American) Purse class: public class Purse { public Purse() { public void addnickels(int count) { public void adddimes(int count) { public void addquarters(int count) { public double gettotal() { public static final double NICKEL_VALUE = 0.05; public static final double DIME_VALUE = 0.1; public static final double QUARTER_VALUE = 0.25; Cohesion There are actually two concepts being referred to by the Purse interface: A purse that holds coins and computes their total The values of the individual coins It would make more sense to have a separate Coin class and have coins responsible for knowing their own values public class Coin { public Coin(double value, String name) { public double getvalue() { Department of Computer Science 12 Department of Computer Science 2

Cohesion Then the Purse class can be simplified: public class Purse { public purse() { public void add(coin acoin) { public double gettotal() { This is clearly a better solution Separates the responsibilities of the purse and the coins Department of Computer Science 13 Many classes need other classes to do their job Eg. the restructured Purse class now depends on the Coin class to determine the total value of the coins in the purse Note that the Coin class does not depend on the Purse class Coins have no idea that they are being collected in purses, and they can carry out their work without ever calling any method in the Purse class Purse Coin Department of Computer Science 14 If many classes of a program depend on each other, then we say that coupling between classes is high Low coupling High coupling Why does coupling matter? If the Coin class changes in the next release of the program, all the classes that depend on it may be affected If the change is drastic, the coupled classes must all be updated Furthermore, if we want to use a class in another program, we have to take with it all the classes on which it depends Thus, we should remove unnecessary coupling between classes Department of Computer Science 16 Accessor method A method that accesses an object and returns some information about it, without changing the object Mutator method A method whose purpose is to modify the state of an object public class BankAccount { private double mbalance; public void deposit(double amount) { mbalance = mbalance + amount; public double getbalance() { return mbalance; Mutator method Department of Computer Science 17 Accessor method Department of Computer Science 18 Department of Computer Science 3

As a rule of thumb, it is a good idea for mutators to have return type void Makes it easy to differentiate between mutators and accessors You can call an accessor method as many times as you like you always get the same answer Does not change the state of the object Makes the behaviour of such a method very predictable Department of Computer Science 19 Some classes have been designed to have only accessor methods Such classes are called immutable Eg. String class once constructed, its contents never change Advantage of an immutable class: it is safe to give out references to its objects freely No code can unexpectedly modify an object Department of Computer Science 20 All instance methods have one implicit parameter a reference to the object containing the method Can be accessed (if necessary) via the this keyword A method may have zero or more explicit parameters ie. those that are passed in as arguments If a method modifies some outside value other than its implicit parameter, we call that modification a side effect ie. a side effect is any kind of observable behaviour outside the object Department of Computer Science 21 public void transfer(bankaccount other, double amount) { withdraw(amount); other.deposit(amount); Modifies an explicit parameter ie another objects state is changed Modifies the implicit parameter ie. this object s state is changed As a rule of thumb, updating an explicit parameter can be surprising to programmers Best to avoid it whenever possible Department of Computer Science 22 Another example of a side effect is output Consider printing a bank balance: System.out.println( The balance is now $ + mysavings.getbalance()); We could simply have a printbalance method instead: public void printbalance() { System.out.println( The balance is now $ + mbalance); Department of Computer Science 23 printbalance would be more convenient when we actually want to print the value However, can t just drop getbalance in favour of printbalance as there cases we might want the value for other purposes printbalance forces strong assumptions on the BankAccount class The message is in English Relies on System.out may not work in an embedded system such as an ATM A method with side effects introduces additional dependencies and thus increases coupling Department of Computer Science 24 Department of Computer Science 4

Side effects cannot be completely eliminated in an OO programming language Can be the cause of surprises and problems and should be minimised when possible Classification of method behaviour: Best: Accessor methods with no changes to any explicit parameters no side effects. Eg: getbalance Good: Mutator methods with no changes to any explicit parameters no side effects. Eg: deposit Fair: Methods that change an explicit parameter: Eg transfer Poor: Methods that change a static field of another class Department of Computer Science 25 Summary Designing good classes is a learned art Applying rules of best practice and common sense can help Look for classes that Have a good descriptive name that is a noun Are cohesive with low coupling (dependencies) Have minimal side effects (externally observable behaviour) More on designing classes in the next lecture Department of Computer Science 26 Readings Section 3.7.3 of Budd Department of Computer Science 27 Department of Computer Science 5