Title: Association Classes and Qualified Associations
1Association Classes and Qualified Associations
- Association Classes
- Reference Course Notes p. C-21 to C-23.
- Association Classes a means of associating data
values with a link (associations). - Association classes are particularly useful for a
many to many multiplicity - Example 1 Student Enrolments
2Association Classes and Qualified Associations
- We can introduce an association class that
contains attribute (data) values that corresponds
to the EnrolsIn link, naming the association
class Registration in the following example (The
association class could also have been called
Enrolment).
There is one instance of Registration for each
Student-Subject registration/enrolment.
3Association Classes and Qualified Associations
4Association Classes and Qualified Associations
- However the following is not
Fred Student
CS1 Subject
Registration mark 57
Registration mark 68
note also the class, Registration, could have
been modeled as an intermediate class and then
there would be no restriction (see lecture 6)
5Association Classes and Qualified Associations
- Example 2 Work Positions
- Another example of the possible use of an
association class fig 6.46 - A person is employed by a company. They can only
be employed by one company. - They have a salary which we need to remember
- Where can we store this bit of data?
(a) store salary with Person
6Association Classes and Qualified Associations
- Example 2 Work Positions
- Another example of the possible use of an
association class fig 6.46 - A person is employed by a company. They can only
be employed by one company. - They have a salary which we need to remember
- Where can we store this bit of data?
(a) or as an association class (i.e. as a
property of the position occupied) see p C-22 for
a discussion
A salary is not an intrinsic property of a
person, the position the person fills is paid.
The salary is a property of the association
(position) rather than of the person or company.
7Association Classes and Qualified Associations
- Qualified Associations Reference Course Notes
p. C-23 to C-25. - Consider the following class diagram
If we were modelling a real world situation then
we would expect each student to be
uniquely identified by an identification number
(i.e. id). The above model doesnt indicate that.
This is quite legal.
8Association Classes and Qualified Associations
- Qualified Associations Reference Course Notes
p. C-23 to C-25. - If we wish to state (in the model) that the
attribute id has to be unique across all student
objects then we could use the idea of a qualified
association between university and student. - The qualifier is an attribute of the association
between two classes - In this case id is an attribute of the
association between University and Student. The
University is responsible for allocating unique
student numbers so the qualifier goes on it. e.g.
Every student at the University has a unique
student number. An id number may be associated
with 0 or 1 student (0 is needed as an id may
not have been assigned to a student yet e.g.
printout of id numbers in advance of being used).
A student is enrolled at one university. A one
to many relationship is implied with qualified
associations e.g. a university can have many
students each with their own unique id number.
9Association Classes and Qualified Associations
- Qualified Associations Reference Course Notes
p. C-23 to C-25. - The University class is responsible for
allocating the student numbers (and ensuring that
they are unique). - To best understand what is happening here
consider the following informal object
type diagram
10Interfaces
- "An Interface is a specialised class declaration
that can declare constants and method
declarations, but not method implementations".
(Java API) - Interface is NOT referring to the user interface
e.g. GUI - An interface is a description of behavior.
Classes that implement the interface must provide
the services (methods) described by the interface
(Budd p401) - Only method definitions NOT implementations may
be provided (e.g. no method body) - A class may implement multiple interfaces
- While a method must be implemented in the class,
they may be skeletons e.g. empty body - The stereotype interface is used above the
interface name in the class diagram to specify
and interface - A dotted line (dependency) with an arrow pointing
to the interface is used to denote the use of an
interface - An optional stereotype use can be added to the
dependency line. Largely it is redundant
11Interfaces
- Example 1 Providing a common interface
- The Switch interface could be used on
ElectricalDevices and other unrelated classes,
giving a common interface (behavior). - Uses
- A class designer may choose to write the Switch
interface because they believe that this is a
common behavior that will be useful in a number
of classes. - A class designer or programmer may choose to
implement the Switch interface because it gives a
class a common look and feel to other classes
that implement the interface. - A programmer inheriting from a class that
implements Switch (or its base class (superclass)
... implements Switch) knows that these methods
are available.
A class diagram showing the Switch interface
12Interfaces
The Switch interface in detail
13Interfaces
- Example 2 Multiple Interfaces
- The following example uses multiple interfaces
(Switch and Controller). - The class GasHeater inherits from the superclass
GasDevice, e.g. a GasHeater is-a type of
GasDevice. However a GasHeater can also be an
ElectricalDevice. e.g. GasHeater inheriting from
the superclasses GasDevice and ElectricalDevice.
Rather than using multiple inheritance (Java,
VB.NET don't support multiple inheritance, C
does) However GasHeater can inherit from the most
appropriate superclasses GasDevice and implement
interfaces that are appropriate Switch and
Controller, thereby giving it behaviors in common
with ElectricalDevices.
14Interfaces
The Controller interface in detail
15Syntax for detailed class descriptions
- Static Methods and Fields
- Static fields/methods apply to the whole class
rather than an instance - Static methods/fields are also called Shared
methods/fields (VB.NET) or Class methods/fields
(Delphi) - Static fields or methods are underlined in a
class diagram. - Static Fields
- Only one instance of a static field exists for a
class, irrespective of the number of instances - Use
- e.g. a field to count the number of instances of
a class
16Syntax for detailed class descriptions
- Static Methods
- Static methods can be called without referring
to an instance of a class - A constructor is a special type of static
method. - Examples of static methods (in Java) are
System.out.println, Integer.parseInt - Static methods may refer to static fields of the
class or call static methods of the class - Static methods may not refer to non-static
fields of the class or call non- static methods
of the class - Use
- general purpose methods that don't need to
access (non- static) fields e.g. Math.sqrt - methods that only access static fields in the
class e.g. getCount() the number of instances
of a class
17Syntax for detailed class descriptions
- Fields and Methods
- (Conventions for specifying attributes and
operations in the class diagram) - Fields (Attributes)
- Format
- access field-name data-type valueaccess
field-namemultiplicity data-type values - The field name is compulsory, the other parts are
optional.Static fields are underlined. - Access types (or visibility)
- - (Private)
- (Protected)
- (Public)
18Syntax for detailed class descriptions
- Fields and Methods
- Conventions for specifying attributes and
operations in the class diagram - Some standard data types are
- Integer
- Double (floating point)
- String
- Char
- Boolean
- Date
- Currency
19Syntax for detailed class descriptions
- Multiplicity
- Attributes can exhibit multiplicity (the default
is exactly one) e.g. - title String
- The following means that some instances of the
class Subject need not store a date of the
examination because there is no exam - exam0..1 Date
- The following means that the names of all staff
delivering a module can be stored (i.e. more than
one) - staff1.. String
20Syntax for detailed class descriptions
- Enumerated Types
- Enumerated types may be specified as follows
21Syntax for detailed class descriptions
- private protected public
22Syntax for detailed class descriptions
- Methods (operations)
- Format
- access method-name(param1 data-type, ...)
return-type - No return type is shown for void methods
(subroutines/procedures).Static methods are
underlined.Abstract methods and classes are
shown in italics
Student
Student()Student(firstName, lastName)setName(
firstName String, lastName String)setTitle(new
Title String)getTitle() Stringenrol(Student)-
getEnterScore() DoublegetCount() Integer
23Syntax for detailed class descriptions
Student
-number Integername Stringtitle
Stringenrolled Boolean False-feesOwing
Currency 0enterScore Doublecampus String
"Bendigo"favouriteColour Colourexam0..1
Datestaff Stringcount Integer 0
Student()Student(firstName, lastName)setName(
firstName String, lastName String)setTitle(new
Title String)getTitle() Stringenrol(Student)-
getEnterScore() DoublegetCount() Integer
24Syntax for detailed class descriptions
- Design level vs Implementation Level Class
Diagrams - Class diagrams can be shown with differing
amounts of detail. Analysis/Design level detail
may omit some details (e.g. parameter type). This
may be appropriate as the details may not be
known or may not be relevant at the current stage
Class diagrams that are appropriate for
implementation show all details relevant to a
programmer e.g. all parameters, data types,
return types, visibility (e.g. public, private)
etc.