Designing Classes
CS239 – Jan 26, 2006
Key points from yesterday’s lab
Enumerated types are abstract data types that define a set of values.
They form a java “collection”, so a for each loop can be used if we want to run through all of the values.
They can be used to declare new variables, which can only take on the values defined by the enum type.
Questions from the worksheet
Designing Classes
Context Class Design process Practice with a small class
Identifying Classes and Objects
The core activity of object-oriented design is determining the classes and objects that will make up the solution
The classes may be part of a class library, reused from a previous project, or newly written
One way to identify potential classes is to identify the objects discussed in the requirements
Objects are generally nouns, and the services that an object provides are generally verbs
Identifying Classes and Objects A partial requirements document:
The user must be allowed to specify each product byits primary characteristics, including its name andproduct number. If the bar code does not match theproduct, then an error should be generated to themessage window and entered into the error log. Thesummary report of all transactions must be structuredas specified in section 7.A.
Of course, not all nouns will correspond toa class or object in the final solution
Identifying Classes and Objects
Remember that a class represents a group (classification) of objects with the same behaviors
Generally, classes that represent objects should be given names that are singular nouns
Examples: Coin, Student, Message
A class represents the concept of one such object
We are free to instantiate as many of each object as needed
Identifying Classes and Objects Sometimes it is challenging to decide whether
something should be represented as a class
For example, should an employee's address be represented as a set of instance variables or as an Address object
The more you examine the problem and its details the more clear these issues become
When a class becomes too complex, it often should be decomposed into multiple smaller classes to distribute the responsibilities
Identifying Classes and Objects
We want to define classes with the proper amount of detail
For example, it may be unnecessary to create separate classes for each type of appliance in a house
It may be sufficient to define a more general Appliance class with appropriate instance data
It all depends on the details of the problem being solved
Identifying Classes and Objects
Part of identifying the classes we need is the process of assigning responsibilities to each class
Every activity that a program must accomplish must be represented by one or more methods in one or more classes
We generally use verbs for the names of methods
In early stages it is not necessary to determine every method of every class – begin with primary responsibilities and evolve the design
Defining the class
Write an abstract (textual) description Create an initial encapsulation Refine the encapsulation Identify constructors Think about the need for private methods Identify helpful overloaded methods Identify helpful overloaded constructors Identify class attributes Identify class behaviors
Example
You are working on a banking system. You have been given the following description of Money objects for the new system.
Money will be represented as dollars and cents with cents neverexceeding 99. Money can be increased or decreased by other Money amounts. Money can be negative or positive values. When displayed, money will be displayed in the usual way ex. $9999999.99 and if negative $99999999.99-. Money amounts can be compared.
Initial Encapsulation – Specs
Nouns-attributes Verbs-behaviors/methods
Money will be represented as dollars and cents with cents neverexceeding 99. Money can be increased or decreased by other Money amounts. Money can have negative or positive values. When displayed, money will be displayed in the usual way ex. $9999999.99 and if negative $99999999.99-. Money amounts can be compared.
Initial Encapsulation
Attributes should be private Accessor (get) methods Mutator (set) methods Other methods to take care of
behaviors.
Initial encapsulation
In your group: Determine your attributes and the
type of each – How will you represent money?
Determine how you will deal with the negative amounts.
Then list the method headers which will represent the listed behaviors.
How many attributes do you have?Refine the attributes
Attributes should to define essential characteristics of the objects.
Local variables should be used for other types of data.
You should have 4 methods defined
What are they? What is passed to the method? What is returned from the method?
Refining the methods
Look for similar behaviors. Are you increasing and decreasing as
completely separate tasks. What is similar about them. Can you write one as a variant of another?
Did you build multiple methods for compare?
Is your display written as toString? Most classes should have one providing a “view” of the object.
Refining the methods
Look for missing behavior Is a method complex and can/should be
broken down into more than one method?
Is there something else within the scope of the definition that may be needed?
Constructors
What would a basic constructor look like?
What will it do to insure that all values are set correctly?
Private – helper methods
Is there anything that might help make the process of building and manipulating Money objects easier?
Is there any process that we don’t want to directly provide to users?
Are there methods that we might overload for different variants
What if we want to increase by a Money amount or simply by a multiplier?
Can these overloaded methods use one another?
(Java will use automatic type conversion if it cannot find an exact match but a similar match – think Math.pow(3,2);) You may not have to build every variant.
Overloaded constructors
Often use a default and an explicit value.
What kind of constructors would be appropriate to Money?
Any class attributes?
Typically, constants, special values. Object counters. Are there any appropriate to this
class?
And are there any class behaviors?
Services that don’t work with a specific object.
Functions that return class attribute values or methods that manipulate class attribute values.
Utilities (think PA1). Conversions Factory methods (build objects of that
type) – static but return an object of the class type.