Class Modeling

Object and Class Concepts

<

Objects

The purpose of class modeling is to describe objects. An object is a concept, abstraction, or thing with identity that has meaning for an application.

Objects usually surface as proper nouns or specific references when you talk to users about a problem. Not every object has to exist physically:

Joe Smith Simplex Company process #7648 the top window simulation run 1234 binary tree 634

Classes

An object is an instance — an occurrence — of a class. A class describes a group of objects that share the same properties (attributes), behavior (operations), relationships, and meaning.

Person Company Process Window

Classes tend to show up as common nouns in problem descriptions, where objects show up as proper nouns. Every Person has a name and birthdate and may work at a job; every Process has an owner, a priority, and a list of required resources.

Class Diagrams

Class Diagram

Graphic notation for classes and their relationships — describes the space of possible objects. Used for both abstract modeling and real program design. A class diagram corresponds to an infinite set of possible object diagrams.

Object Diagram

Shows specific, individual objects and how they're linked at one moment. Useful for documenting test cases and walking through concrete examples.

Figure 3.1 shows a class on the left and three of its instances on the right.

Class
Person
Objects
JoeSmith:Person
MarySharp:Person
:Person

Figure 3.1 — A class and its objects. The UML object symbol is a box reading objectName:ClassName, both underlined. An unnamed instance simply omits the object name, leaving :Person.

Note the naming convention used throughout the book: multiword names run together with an internal capital letter (JoeSmith) rather than a space or underscore. It's a popular OO convention, not a UML requirement.

Values and Attributes

A value is a piece of data. An attribute is a named property of a class describing a value held by every object of that class.

Class with Attributes
Person
name : string birthdate : date
Objects with Values
JoeSmith:Person
name="Joe Smith" birthdate=21 Oct 1983
MarySharp:Person
name="Mary Sharp" birthdate=16 Mar 1950

Figure 3.2 — Attributes are listed in the class box's second compartment, optionally typed after a colon. Object boxes list the actual value after an equal sign.

Operations and Methods

An operation is a function or procedure that may be applied to or by the objects in a class. hire, fire, and payDividend are operations on Company; open, close, hide, and redisplay are operations on Window. All objects in a class share the same set of operations.

Operation vs. method. A method is the class-specific implementation of an operation. File might declare a single print operation, implemented by separate methods for ASCII files, binary files, and digitized pictures — different code, same logical task, same generic name.

name, birthdate, changeJob, and changeAddress are all features of Person — feature is the umbrella term for "attribute or operation."

Person
namebirthdate
changeJob()changeAddress()
File
fileNamesizeInByteslastUpdate
print()
GeometricObject
colorposition
move (delta: Vector) select (p: Point): Boolean rotate (in angle: float = 0.0)

Figure 3.4 — Operations sit in the class box's third compartment. move takes a Vector; select takes a Point and returns a Boolean; rotate takes an input float defaulting to 0.0.

Object boxes never list operations — behavior doesn't vary between objects of the same class, so showing it per-object would only add noise.

Summary of Notation for Classes

A class box has up to three stacked compartments — name, attributes, operations — each optional beyond the name itself.

ClassName
attributeName1 : dataType1 = defaultValue1 attributeName2 : dataType2 = defaultValue2
operationName1 (argumentList1) : resultType1 operationName2 (argumentList2) : resultType2

Figure 3.5 — General notation for a class box, three compartments deep.