Chapter 7 Object-Oriented Design Concepts

Slides:



Advertisements
Similar presentations
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Enumerated Types Define a new data type and list all possible values: enum Season {winter, spring, summer, fall} Then the new type can be used to declare.
Chapter 4: Writing Classes
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Fall 2007CS 2251 Enum Types from Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
Design: Coupling and Cohesion How to write classes in a way that they are easily understandable, maintainable and reusable.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Writing Classes (Chapter 4)
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
The this Reference The this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
Chapter 6 Object-Oriented Design Part 3. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Outline Enumerated Types Revisited Method Design Testing.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Layouts, Types, & Switches Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Static class members.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
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.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
SEEM Java – Basic Introduction, Classes and Objects.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
University of Turkish Aeronautical Association Computer Engineering Department CENG 112 COMPUTER PROGRAMMING 2 Tansel Dökeroglu, Ph.D.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter VII: Arrays.
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Objects First with Java
Passing Objects to Methods
More Object Oriented Programming
Object Oriented Programming
Object Based Programming
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes, Encapsulation, Methods and Constructors (Continued)
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming Review
Object-Oriented Design Part 2
Presentation transcript:

Chapter 7 Object-Oriented Design Concepts Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Copyright © 2012 Pearson Education, Inc.

OO Class Terminology Interface (conceptual definition): Set of public method signatures for a class Defines how other classes can use it Implementation: Code used to perform the desired functionality defined by the interface Includes private fields & methods & public method bodies Interface (Java construct definition): Special class with no source code, no constructors Used to enforce good design Copyright © 2012 Pearson Education, Inc.

What is good design? Making code easy to modify, test, & debug by localizing changes The less code is changed, the less chance for bugs to be introduced reduce amount of testing Goal: design code so changes can be made to small, independent units Code duplication: copying & pasting the same (or almost the same) code multiple times Why we avoid it: makes code modification difficult we might not modify all duplicates Copyright © 2012 Pearson Education, Inc.

How can we achieve good design? Cohesion: each code unit (method/class) should be responsible for 1 and only 1 well-defined task Why we want high cohesion: localize changes to one well-defined place Cohesive code is easier for others to understand, & modify Cohesive code is easier to reuse, thus reducing code duplication Responsibility-driven design (RDD): store data where it’s predominantly manipulated Coupling: the degree to which 2 classes are inter-dependent When the implementation of a class changes, other classes should not be affected (when the interface changes, they might be) Why we avoid tight-coupling: more work to modify code; can’t just change 1 class, have to change all the tightly-coupled classes Encapsulation: what a class/method does is visible to the outside (to other classes) by not how it does it Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Step 2: understand how the code does it (zipped project) Step 3: can we improve the code’s design? (Tasks.docx) You should be migrating from ZuulOrignalUML to ZuulEnumUML Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Map: Step 2: understand how the code does it (zipped project) Step 3: can we improve the code’s design? (Tasks.docx) Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Step 2: understand how the code does it (zipped project) Step 3: can we improve the code’s design? (Tasks.docx) You should be migrating from ZuulOrignalUML to ZuulEnumUML Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Step 2: understand how the code does it (zipped project) Code consists of 5 classes: Game: runs game, puts all the other pieces (classes) together with the game’s logic Parser: processes user input Room: manages room info [description, exits] CommandWords: manages valid commands in game & supports Parser Command: manages info about a command entered by a user [command + option] Step 3: can we improve the code’s design? (Tasks.docx) Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Step 2: understand how the code does it (zipped project) Step 3: can we improve the code’s design? (Tasks.docx) You should be migrating from ZuulOrignalUML to ZuulEnumUML Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Step 2: understand how the code does it (zipped project) Step 3: can we improve the code’s design? (Tasks.docx) Can you find any duplicated code? Copyright © 2012 Pearson Education, Inc.

Design Concepts in Practice: World of Zuul Command-line adventure game Step 1: understand what the code does (run it) Step 2: understand how the code does it (zipped project) Step 3: can we improve the code’s design? (Tasks.docx) Can you find any duplicated code? Improve design, not functionality Remove code duplication in Game between printWelcome() & goRoom() by refactoring printLocationInfo() Copyright © 2012 Pearson Education, Inc.

Code duplication in Game Copyright © 2012 Pearson Education, Inc.

Copyright © 2012 Pearson Education, Inc.

Refactor Copyright © 2012 Pearson Education, Inc.

Exploring Design through Modification Tasks How could we change the design of the code to make the following tasks easier? Add another direction of movement to NESW: Up/Down Add a new room: cellar Add an additional command word Add a different command language besides English *See Tasks.docx Don’t make change, just walk through all the steps – how could we change the design of the code to make these tasks easier? Copyright © 2012 Pearson Education, Inc.

Homework Practice design concepts with the exercises on BB (Designing Classes Practice under MPL) Think about Project 2 Take from BB quiz last year Copyright © 2012 Pearson Education, Inc.

Review Material Copyright © 2012 Pearson Education, Inc.

Method Overloading The compiler determines which method is being invoked by analyzing the parameters float tryMe(int x) { return x + .375; } result = tryMe(25, 4.32) Invocation float tryMe(int x, float y) { return x*y; } Copyright © 2012 Pearson Education, Inc.

Method Overloading The println method is overloaded: println (String s) println (int i) println (double d) and so on... The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total); Copyright © 2012 Pearson Education, Inc.

enum Season {winter, spring, summer, fall} Enumerated Types In Chapter 3 we introduced enumerated types, which define a new data type and list all possible values of that type: enum Season {winter, spring, summer, fall} Once established, the new type can be used to declare variables Season time; The only values this variable can be assigned are the ones established in the enum definition Copyright © 2012 Pearson Education, Inc.

Enumerated Types An enumerated type definition is a special kind of class The values of the enumerated type are objects of that type For example, fall is an object of type Season That's why the following assignment is valid: time = Season.fall; Copyright © 2012 Pearson Education, Inc.

Enumerated Types An enumerated type definition can be more interesting than a simple list of values Because they are like classes, we can add additional instance data and methods We can define an enum constructor as well Each value listed for the enumerated type calls the constructor See Season.java See SeasonTester.java Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Season.java Author: Lewis/Loftus // // Enumerates the values for Season. public enum Season { winter ("December through February"), spring ("March through May"), summer ("June through August"), fall ("September through November"); private String span; //----------------------------------------------------------------- // Constructor: Sets up each value with an associated string. Season (String months) span = months; } // Returns the span message for this value. public String getSpan() return span; Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // SeasonTester.java Author: Lewis/Loftus // // Demonstrates the use of a full enumerated type. public class SeasonTester { //----------------------------------------------------------------- // Iterates through the values of the Season enumerated type. public static void main (String[] args) for (Season time : Season.values()) System.out.println (time + "\t" + time.getSpan()); } Copyright © 2012 Pearson Education, Inc.

Output winter December through February spring March through May summer June through August fall September through November //******************************************************************** // SeasonTester.java Author: Lewis/Loftus // // Demonstrates the use of a full enumerated type. public class SeasonTester { //----------------------------------------------------------------- // Iterates through the values of the Season enumerated type. public static void main (String[] args) for (Season time : Season.values()) System.out.println (time + "\t" + time.getSpan()); } Copyright © 2012 Pearson Education, Inc.

Enumerated Types Every enumerated type contains a static method called values that returns a list of all possible values for that type The list returned from values can be processed using a for-each loop An enumerated type cannot be instantiated outside of its own definition A carefully designed enumerated type provides a versatile and type-safe mechanism for managing data Copyright © 2012 Pearson Education, Inc.

Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of the Math class are static: result = Math.sqrt(25) Variables can be static as well Determining if a method or variable should be static is an important design decision Copyright © 2012 Pearson Education, Inc.

The static Modifier We declare static methods and variables using the static modifier It associates the method or variable with the class rather than with an object of that class Static methods are sometimes called class methods and static variables are sometimes called class variables Let's carefully consider the implications of each Copyright © 2012 Pearson Education, Inc.

Static Variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class is first referenced All objects instantiated from the class share its static variables Changing the value of a static variable in one object changes it for all others Copyright © 2012 Pearson Education, Inc.

Static Methods public class Helper { public static int cube (int num) return num * num * num; } Because it is declared as static, the cube method can be invoked through the class name: value = Helper.cube(4); Copyright © 2012 Pearson Education, Inc.

Static Class Members The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static – it is invoked by the Java interpreter without creating an object Static methods cannot reference instance variables because instance variables don't exist until an object exists However, a static method can reference static variables or local variables Copyright © 2012 Pearson Education, Inc.

Static Class Members Static methods and static variables often work together The following example keeps track of how many Slogan objects have been created using a static variable, and makes that information available using a static method See SloganCounter.java See Slogan.java Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // SloganCounter.java Author: Lewis/Loftus // // Demonstrates the use of the static modifier. public class SloganCounter { //----------------------------------------------------------------- // Creates several Slogan objects and prints the number of // objects that were created. public static void main (String[] args) Slogan obj; obj = new Slogan ("Remember the Alamo."); System.out.println (obj); obj = new Slogan ("Don't Worry. Be Happy."); continue Copyright © 2012 Pearson Education, Inc.

obj = new Slogan ("Live Free or Die."); System.out.println (obj); continue obj = new Slogan ("Live Free or Die."); System.out.println (obj); obj = new Slogan ("Talk is Cheap."); obj = new Slogan ("Write Once, Run Anywhere."); System.out.println(); System.out.println ("Slogans created: " + Slogan.getCount()); } Copyright © 2012 Pearson Education, Inc.

Output Remember the Alamo. Don't Worry. Be Happy. Live Free or Die. Talk is Cheap. Write Once, Run Anywhere. Slogans created: 5 continue obj = new Slogan ("Live Free or Die."); System.out.println (obj); obj = new Slogan ("Talk is Cheap."); obj = new Slogan ("Write Once, Run Anywhere."); System.out.println(); System.out.println ("Slogans created: " + Slogan.getCount()); } Copyright © 2012 Pearson Education, Inc.

//******************************************************************** // Slogan.java Author: Lewis/Loftus // // Represents a single slogan string. public class Slogan { private String phrase; private static int count = 0; //----------------------------------------------------------------- // Constructor: Sets up the slogan and counts the number of // instances created. public Slogan (String str) phrase = str; count++; } continue Copyright © 2012 Pearson Education, Inc.

//----------------------------------------------------------------- continue //----------------------------------------------------------------- // Returns this slogan as a string. public String toString() { return phrase; } // Returns the number of instances of this class that have been // created. public static int getCount () return count; Copyright © 2012 Pearson Education, Inc.

Quick Check Why can't a static method reference an instance variable (i.e., non-static field)? Copyright © 2012 Pearson Education, Inc.

Quick Check Why can't a static method reference an instance variable (i.e., non-static field)? Because instance data is created only when an object is created. You don't need an object to execute a static method. And even if you had an object, which object's instance data would be referenced? (remember, the method is invoked through the class name) Copyright © 2012 Pearson Education, Inc.

Review: the this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference is used inside a method called tryMe, which is invoked as follows: obj1.tryMe(); obj2.tryMe(); In the first invocation, the this reference refers to obj1; in the second it refers to obj2 Copyright © 2012 Pearson Education, Inc.

The this reference The this reference can be used to distinguish the instance variables of a class from corresponding method parameters with the same names The constructor of the Account class from Chapter 4 could have been written as follows: public Account (String name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; } Copyright © 2012 Pearson Education, Inc.