Designing Classes CS239 – Jan 26, 2006. Key points from yesterday’s lab Enumerated types are...

25
Designing Classes CS239 – Jan 26, 2006

Transcript of Designing Classes CS239 – Jan 26, 2006. Key points from yesterday’s lab Enumerated types are...

Page 1: 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.

Designing Classes

CS239 – Jan 26, 2006

Page 2: 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.

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.

Page 3: 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.

Questions from the worksheet

Page 4: 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.

Designing Classes

Context Class Design process Practice with a small class

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

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.

Page 13: 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.

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.

Page 14: 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.

Initial Encapsulation

Attributes should be private Accessor (get) methods Mutator (set) methods Other methods to take care of

behaviors.

Page 15: 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.

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.

Page 16: 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.

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.

Page 17: 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.

You should have 4 methods defined

What are they? What is passed to the method? What is returned from the method?

Page 18: 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.

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.

Page 19: 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.

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?

Page 20: 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.

Constructors

What would a basic constructor look like?

What will it do to insure that all values are set correctly?

Page 21: 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.

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?

Page 22: 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.

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.

Page 23: 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.

Overloaded constructors

Often use a default and an explicit value.

What kind of constructors would be appropriate to Money?

Page 24: 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.

Any class attributes?

Typically, constants, special values. Object counters. Are there any appropriate to this

class?

Page 25: 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.

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.