2007 ACS-3913 Ron McFadyen 1
Class Diagram
See Schaum’s UML Outline, especially chapters 4, 5, 6, 7
2007 ACS-3913 Ron McFadyen 2
Class
Represented by a rectangle with possibly 3 compartments
Customer
Customer
NameAddress
Customer
NameAddress
getName()checkCreditRating()
Customer
getName()checkCreditRating()
Class name
attributes
operations
operations
attributes
Class nameClass name
Class name
2007 ACS-3913 Ron McFadyen 3
Class
«Singleton»dbFacade
Some classes are stereotyped:
Later in the course we study the Singleton design pattern.
Some methodologies have 3 stereotypes for “analysis” classes: boundary, control, entity
2007 ACS-3913 Ron McFadyen 4
Attributes
an object contains data which are defined as part of the Class definition
examples:• Students have names, addresses, etc; • Courses have titles, descriptions, prerequisite information.
Rectangle
corner: Point
Student
nameaddress
Level of detail present will depend on whether you are in analysis or design, and your purposes at the time
2007 ACS-3913 Ron McFadyen 5
Attributes
To what degree is an attribute visible to other classes?Private –Public +Protected #Package ~
Student
-name-address
2007 ACS-3913 Ron McFadyen 6
Attributes
Default values =Derived values /Multiplicity [ ]Ordering {ordered}Uniqueness {unique}
Invoice
-date:Date = today-/total: Currency-payments[0..*]: Currency
Student
-name-address[1..3] {unique}
2007 ACS-3913 Ron McFadyen 7
Operations. What are the responsibilities of a class? What can it do?
Visibility
Parameters
Signature the name, parameters, and return type of the operation
Student
+getName()+getGPA(term :Term, gpaType: String)
2007 ACS-3913 Ron McFadyen 8
Associations
• correspond to verbs expressing a relationship between classes
• example a Library Member borrows a Copy of a Book
•Navigability•A directed association show that messages will be sent in that direction
•Multiplicities• we indicate via multiplicities the range of allowable cardinalities for participation in an association• examples: 1, 1..*, 0..*, 1..3
2007 ACS-3913 Ron McFadyen 9
Associations
• Names and roles
• you can name the relationship and indicate how to read it• you can give role names for participating objects
Person CompanyWorks for1..* 1
employer employee
The role of a Person in this relationship The role of a Company in this relationship
The name of the relationship and thedirection for reading the name
2007 ACS-3913 Ron McFadyen 10
Associations
•example: A duck uses a flying behavior
Duck FlyBehavior1uses
2007 ACS-3913 Ron McFadyen 11
Associations
• example: An employee is supervised by an employee
*
0,1Employee
reports to
supervised
supervisor
A reflexive association: an instance of this association involves two Employee objects (two objects from the same class).
2007 ACS-3913 Ron McFadyen 12
Inner Class
See Head First, Chapter 2, page 73Other sources:
Applying UML and patterns; Ch 26 Object-oriented design & patterns; Ch 5
SwingObserverExample AngelListener Angel listens to
DevilListener Devil listens to
+
+
+It is common to use to indicate an inner class
2007 ACS-3913 Ron McFadyen 13
Navigability
The association below is directed. The arrow indicates the direction in which messages are sent. Note this means that a duck will “know” its flying behaviour. In the implementation of a duck there is need for an attribute that is a reference to a flying behaviour object.
Duck FlyBehavior 1uses
2007 ACS-3913 Ron McFadyen 14
Navigability:
An association line with a navigability adornment indicates that an object of one class (A) is connected uni-directionally to an object of another class (B)
An object of class A can navigate to an object of class B.
•I.e. an object of class A needs to know the B object, but not the other way around.
•An instance of A can send a message to an instance of B, the instance can respond, but doesn’t initiate.
An association line that has no specific navigation indicated, is considered to be bi-directional
In an OO language,
an A will have an
attribute that
references a B instance
A
b: B
B
2007 ACS-3913 Ron McFadyen 15
Association Class
Business rule: a person contracts their services to a company
Association Class: a modeling element that is both an association and a class. It has attributes, operations, multiplicities, etc. It can participate in other relationships. Note the dashed line.
Company Person
Contract
startDate
* *
2007 ACS-3913 Ron McFadyen 16
Many to many associations
•likely candidate for an Association Class
Limitation: each company object can be associated to a person object only once. If we wanted to provide for a person to be contracted to the same company more than once, we would need to promote Contract to be a regular class. How do we model this?
Company Person
Contract
startDate
* *
2007 ACS-3913 Ron McFadyen 17
Reflexive Association
An association involving the same class more than once.
marries
Person1
1
marries
Person*
*
?
2007 ACS-3913 Ron McFadyen 18
Reflexive Association
An association involving the same class more than once.
comprises
Part*
*
2007 ACS-3913 Ron McFadyen 19
Interfaces
An interface is special type of class that cannot be instantiated. An application can never instantiate an interface.
An interface defines a set of public attributes and operations that some class must use (depends on)
There is no behaviour defined, no method coded
Some other class inherits the interface and provides the implementation (generalization, inheritance)
2007 ACS-3913 Ron McFadyen 20
Interfaces
<<interface>>FlyBehavior
FlyNoWay
fly()
These classes implement the fly operation
FlyWithWings
fly() fly()
The interface named FlyBehavior comprises the fly operation
2007 ACS-3913 Ron McFadyen 21
Abstract Classes
An abstract class is a special type of class in a class hierarchy that cannot be instantiated. An application can never instantiate an abstract class and so must instantiate objects at a more specialized level in the hierarchy.
An abstract class defines a set of attributes and operations that some class must use (depends on)
If an operation is not abstract, then its behaviour is defined in the abstract class, i.e. there is an implementation, but this may be overriden in a more specialized subclass.
2007 ACS-3913 Ron McFadyen 22
Abstract Classes
Duck
RubberDuck
performfly()swim()display()
The swim operation is overridden in RubberDuck.
Both subclasses have implementations of display().
MallardDuck
display() swim()display()
The abstract class has its name in italics.
The performFly() and swim() operations are defined, but display() is not defined (it is an abstract operation)