chapter 8 designing classes n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 8: Designing Classes PowerPoint Presentation
Download Presentation
Chapter 8: Designing Classes

Loading in 2 Seconds...

play fullscreen
1 / 20

Chapter 8: Designing Classes

0 Views Download Presentation
Download Presentation

Chapter 8: Designing Classes

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Chapter 8:Designing Classes

  2. 8.1 Choosing Classes • A class represents a single concept from the problem domain • Name for a class should be a noun that describes the concept • Concepts from mathematics: • Point • Rectangle • Ellipse • Concepts from real life: BankAccount CashRegister • Method names should be verbs. e.g. print, draw, sort,…

  3. Choosing Classes • Actors (ending in -er, -or) – are objects that do some kinds of work for you. e.g. Scanner Random // better name: RandomNumberGenerator • Utility classes – not objects, only static methods and constants Math • Program starters: only have a main method • Don't turn actions into classes:Paycheck is a better name than ComputePaycheck

  4. Self Check 8.1 Answer: Look for nouns in the problem description. What is the rule of thumb for finding classes?

  5. What classes would you use for the following problem description:Users place coins in a vending machine and select a product by pushing a button. If the inserted coins are sufficient to cover the purchase price of the product, the product is dispensed and change is given. Otherwise, the inserted coins are returned to the user. • Answer: We might wish to make each of the following into classes: • User • VendingMachine • Button • Coin • Product

  6. Self Check 8.2 Your job is to write a program that plays chess. Might ChessBoard be an appropriate class? How about MovePiece? Answer:ChessBoard would be reasonable, but not MovePiece.

  7. 8.2 Cohesion • A class should represent a single concept • The public interface of a class is cohesive if all of its features are related to the concept that the class represents • This class lacks cohesion: public class CashRegister { public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies) . . . public static final double NICKEL_VALUE = 0.05; public static final double DIME_VALUE = 0.1; public static final double QUARTER_VALUE = 0.25; . . . } • This class represents both the concept of a cash register, along with the concept of the value of coins (and these values are specific to North America)

  8. Cohesion CashRegister, as described above, involves two concepts: cash register and coin Solution: Make two classes: public class Coin { public Coin(double aValue, String aName) { . . . } public double getValue() { . . . } . . . }public class CashRegister { public void enterPayment(int coinCount, Coin coinType) { . . . } . . . }

  9. Minimize Dependencies • A class depends on another if it uses objects of that class • CashRegister depends on Coin to determine the value of the payment • Coin does not depend on CashRegister • Minimize dependencies to minimize the impact of interface changes • To visualize relationships draw class diagrams • UML: Unified Modeling Language. Graphical notation for object-oriented analysis and design

  10. Minimize Dependencies The Coin class does not depend on the CashRegister class.

  11. Best to decouple input/output from the work of your classes • Place the code for producing output or consuming input in a separate class.

  12. Self Check 8.4 Answer: None of the coin operations require the CashRegister class. Why does the Coin class not depend on the CashRegister class?

  13. Self Check 8.5 Why should dependencies be minimized between classes? Answer: If a class doesn't depend on another, it is not affected by interface changes in the other class.

  14. Accessors, Mutators and Immutable Classes • Accessor: does not change the state of the implicit parameter double balance = account.getBalance(); • Mutator: modifies the object on which it is invoked account.deposit(1000); • Immutable class: has no mutator methods (e.g., String)String name = "John Q. Public"; String uppercased = name.toUpperCase(); // name is not changed • It is safe to give out references to objects of immutable classes; no code can modify the object at an unexpected time

  15. Self Check 8.6 Is the substring method of the String class an accessor or a mutator? Answer: It is an accessor – calling substring doesn't modify the string on which the method is invoked. In fact, all methods of the String class are accessors.

  16. Self Check 8.7 Is the Rectangle class immutable? Answer: No – translate is a mutator.

  17. Minimizing Side Effects A side effect of a method is any externally observable data modification. Mutator methods have a side effect, namely the modification of the implicit parameter.

  18. Minimizing Side Effects • In general, a method should not modify its parameter variables. //Computes the total balance of the given accounts. // @param accounts a list of bank accounts public double getTotalBalance(ArrayList<String> accounts) { double sum = 0; while (studentNames.size() > 0) { BankAccount account = accounts.remove(0); // Not recommended sum = sum + account.getBalance(); } return sum; } • Such a side effect would not be what most programmers expect.

  19. Minimizing Side Effects • The following method mutates the System.out object, which is not a part of the BankAccount object. public void printBalance() // Not recommended { System.out.println("The balance is now $" + balance); } • That is a side effect.

  20. Consistency While it is possible to eat with mismatched silverware, consistency is more pleasant.