Classes Redux - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Classes Redux

Description:

Actors. Do some kinds of work for you. Scanner class. Rarely does a class have no objects ... methods, follow a consistent scheme for their names and parameters ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 26
Provided by: Mik7212
Category:
Tags: actors | classes | names | redux

less

Transcript and Presenter's Notes

Title: Classes Redux


1
Classes Redux
  • 10-31-05

2
Choosing Classes
  • Should be nouns
  • Methods are verbs
  • Entities
  • Should represent a simple concept
  • Point, ellipse, rectangle
  • BankAccount, CashRegister

3
Choosing Classes
  • Actors
  • Do some kinds of work for you
  • Scanner class
  • Rarely does a class have no objects
  • Utility class
  • Math
  • Classes with only a main method

4
Not a Good Class
  • Cant tell from the class name what an object of
    the class is suppose to do
  • Trying to turn an action into a class
  • These are generally methods

5
Cohesion
  • A class should represent a single concept
  • The public methods and constants should be
    cohesive
  • All interface features should be closely related
    to the single concept that the class represents

6
Coupling
  • Many classes need other classes in order to do
    their jobs
  • Dependence can be depicted in UML
  • If many classes of a program depend on each
    other, coupling is high
  • If there are few dependencies between classes,
    then coupling is low

7
Consistency
  • When you have a set of methods, follow a
    consistent scheme for their names and parameters
  • messageDialog inputDialog have inconsistent
    parmaters (null)
  • Inconsistencies are generally not a fatal flaw,
    but they are an annoyance

8
InConsistency
  • Public class CashRegister
  • public void enterPayment(int dollars, int
    quarters, int dimes, int nickels, int pennies)
  • public static final double NICKLE_VALUE 0.05
  • public static final double DIME_VALUE 0.10
  • public static final double QUARTER_VALUE 0.25

9
Better Consistency
  • public class Coin
  • public Coin(double aValue, String aName)
  • .
  • public double getValue()
  • ..

10
Better Consistency
  • Public class CashRegister
  • public void enterPayment(int coinCount, Coin
    coinType)
  • .

11
Accessors, Mutators, andImmutable Classes
  • Mutator method modifies the object
  • Accessor method retrieves information from the
    object
  • Classes with just accessor methods are called
    immutable classes
  • Safe to be used anywhere in a program

12
Side Effect
  • A side effect of a method is any kind of
    modification of data that is observable outside
    of the method
  • Updating an explicit parameter can be surprising
    to programmers, and it is best to avoid it
    whenever possible

13
Implicit ExplicitMethod Parameters
  • Implicit the object on which the method is
    invoked
  • Explicit enclosed in the parenthesis

14
Minimize Side Effects
  • In an ideal world, all methods would be accessors
  • Mutator methods with no changes to any explicit
    parameter acceptable side effect
  • Methods that change an explicit parameter
  • Should be avoided
  • Methods that change another object
  • Not good

15
Precondition
  • Is a requirement that the caller of a method must
    obey
  • Requires that a method is only called when it is
    in the appropriate state
  • If a method is called in violation of a
    precondition, the method is not responsible for
    computing the correct result
  • Needs to be documented clearly

16
Preconditions
  • The method is free to do if a precondition is not
    fulfilled
  • Two typical strategies
  • Throw an exception
  • Error handling
  • Work under the assumption that the preconditions
    are fulfilled

17
Assertion
  • Is an condition that y0u believe to be true at
    all times in a particular program location
  • An assertion check tests whether an assertion is
    true
  • Typically tests a precondition
  • If the assertion fails, and assertion testing is
    enabled then the program terminates with an
    AssertionError

18
Assertion
  • public double deposit(double amount)
  • assert amount gt 0
  • balance balance amount

19
Bad Idea
  • public void deposit(double amount)
  • if (amount lt 0) return
  • balance balance amount
  • // legal but will lead to disaster

20
Postconditions
  • The return value is computed correctly
  • The object is in a certain state after the method
    call is completed

21
Static Methods
  • A method that is not invoked on an object
  • Also called a class method
  • Math.sqrt(x)
  • You want to encapsulate some computation that
    involves only numbers.
  • Numbers are not objects
  • x.sqrt() not legal in Java

22
Static Fields
  • Sometimes you need to store values outside any
    particular object
  • Class field
  • Single variable(location) for the entire class
  • Every method in the class can access its static
    field

23
Initializing a Static Field
  • Cant be done in a method
  • Do nothing (use defaults)
  • 0 for numbers
  • false for Booleans
  • null for objects

24
Initializing a Static Field
  • Use an explicit initializer
  • public class BankAccout
  • private stat int last AccountNumber 1000

25
Initializing Static Fields
  • Like instance fields, static fields should always
    be declared as private to ensure that the methods
    of other classes do not change their values
  • Exception static constants
Write a Comment
User Comments (0)
About PowerShow.com