Presentation

Download Report

Transcript Presentation

Designing a class
Kinds of classes
 Utility classes – Math (Do something)
 Object classes – ExpressionParser, Scanner (Are
something)
 Hybrid classes – Integer (Are something but contain
helpful utilities)
Steps
 Write a description (or use one provided)
 Create an initial encapsulation
 Refine the encapsulation
 Identify helpful constructors
 Identify helpful overloaded constructors
 Think about the need for private methods
 Identify helpful overloaded methods
 Identify class level attributes
 Identify class level behaviors
Description
 Should include what we know about the object data
 Should include what the object should be able to do
 If the description is not complete, you can’t create
the class.
Initial Encapsulation
 Identify nouns and verb phrases in the description.
 Nouns are data. What data types? Which are the
important nouns. Which ones are pertinent to the
problem?
 Verbs are the methods. What parameters do they
need? What do they need to return?
Money
 The money class will represent US Money amounts. Once




created a particular Money object cannot change.
We need to be able to add two Money objects to get a third,
subtract two Money objects, multiply a money object by a
value (such as .05) to get a new Money amount and divide a
Money object by a value (such as 2.5).
We must also be able to compare Money objects to determine
which is larger, small or if they are the same.
We must be able to display a Money object as normal dollars
and cents.
Money may only be a whole number of cents. In other words,
Money object may be 3 dollars and 12 cents, but not 3 dollars
and 12.5 cents.
Initial encapsulation
 On your whiteboards, begin writing the class. You
should carefully think about the instance variables
that you need. How will you internally store money
values? (What does a Money object look like?)
 What methods are directly suggested by the
description? (Don’t worry about constructors at this
point). (What should a Money object do?)
Refinement
 Do the attributes really reflect the state of the object?
Are there any that would be used in only one
method?
 Are there any that represent derivations?
 If any of the data changes, would we need another
method. For example, rather than storing derived
data, perhaps we need a method for that.
Identify constructors
 How will we want to build new Money objects?
 What makes sense for the user of this class to do?
Identify any helpful overloaded constructors
 Should we have a default constructor?

Not automatic if any other constructor is defined
 Should we have multiple explicit value constructors?

Are there multiple ways of initializing data
 Are there methods that can help the constructor do it’s
job?

“Check” methods to check for validity
 Constructors can call other constructors.


Stock.java
CopyConDemo.java
Do we need any helper methods?
 These can be methods that would be “utility”
methods to users of the class or could be a helper
method for other methods in the class.
 Private if only used inside of the class; public if a
utility.
Identify overloaded methods
 Different parameter types may have subtle
differences.
 We may want to provide different interfaces if, for
example, we have a similar function but want to pass
in different parameters.
 Or, if we want to allow the possibility of different
numbers of parameters (min method).
Variable length parameter lists
 May obviate the need to have overloaded methods in
the case of cardinality (handle 2, handle 3, handle
more).
 See VarargsDemo1.java
Class attributes?
 Helpful constants, special values
 Common values used across all objects
 Object counters
 What might we want to include that are “constant”
money amounts?