export_java chapter 8 Flashcards
what is the class represent ?
A class represents a single concept from the problem domain
what should be the name of the class ?
Name for a class should be a noun that describes concept
Concepts from mathematics:
Point
Rectangle
Ellipse
what are actors ?
Actors (end in -er, -or)– objects do some kinds of work for you:
Scanner
Random // better name: RandomNumberGenerator
what are the utilty classes ?
no objects, only static methods and constants:
Mathition
what are the program saters ?
only have a main method
Don’t turn actions into classes
Paycheck is a better name than ComputePaycheckion
What is the rule of thumb for finding classes?
Answer: Look for nouns in the problem description.
Your job is to write a program that plays chess. Might
ChessBoard be an appropriate class? How about MovePiece?
Answer: Yes (ChessBoard) and no (MovePiece).
what is 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; ... }
solutions: • 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) { ... } ... }
what is coupling ?
• 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
• High coupling
Many class dependencies
Minimize coupling to
o minimize the impact of interface changes
points of coupling ?
• To visualize relationships draw class diagrams
• UML:
Unified Modeling Language
• Notation for object-oriented analysis and design
Dependency
https://s3.amazonaws.com/classconnection/655/flashcards/7082655/png/image3ergqx-14A3F6ADC191295129F.png
High and Low Coupling Between Classes
https://s3.amazonaws.com/classconnection/655/flashcards/7082655/png/imageu4cdqx-14A3F72369F0E0E1A92.png
Why is the CashRegister class from Chapter 4 not cohesive?
Answer: Some of its features deal with payments, others with
coin values.
Why does the Coin class not depend on the CashRegister class?
Answer: None of the Coin operations require the CashRegister
class.
Why should coupling be minimized between classes?
Answer: If a class doesn’t depend on another, it is not affected by interface changes in the other class.
• Accessor:
Does not change the state of the implicit parameter:
double balance = account.getBalance();