Class Model
Class Model
Chapter 5:
Modelling with Classes
Lecture 5
226
UML diagrams
• Class diagrams
—describe classes and their relationships
• Interaction diagrams
—show the behaviour of systems in terms of how objects
interact with each other
• State diagrams and activity diagrams
—show how systems behave internally
• Component and deployment diagrams
—show how the various components of systems are
arranged logically and physically
227
UML features
228
What constitutes a good model?
A model should
• use a standard notation
• be understandable by clients and users
• lead software engineers to have insights about the system
• provide abstraction
229
230
Classes
231
232
Labelling associations
233
• Many-to-one
—A company has many employees,
—An employee can only work for one company.
- This company will not store data about the moonlighting activities
of employees!
—A company can have zero employees
- E.g. a ‘shell’ company
—It is not possible to be an employee unless you work for a
company
worksFor 1
Employee * Company
234
Analyzing and validating associations
• Many-to-many
—An assistant can work for many managers
—A manager can have many assistants
—Assistants can work in pools
—Managers can have a group of assistants
—Some managers might have zero assistants.
—Is it possible for an assistant to have, perhaps temporarily,
zero managers?
Assistant * 1..** Manager
supervisor
235
• One-to-one
—For each company, there is exactly one board of directors
—A board is the board of only one company
—A company must always have a board
—A board must always be of some company
1 1
236
Analyzing and validating associations
237
238
Association classes
239
Reflexive associations
240
Directionality in associations
241
5.4 Generalization
242
Avoiding unnecessary generalizations
Inappropriate hierarchy of
classes, which should be
instances
243
245
246
Avoiding having instances change class
247
248
Associations versus generalizations in
object diagrams
• Associations describe the relationships that will exist between
instances at run time.
—When you show an instance diagram generated from a
class diagram, there will be an instance of both classes
joined by an association
249
250
When to use an aggregation
251
Composition
252
Aggregation hierarchy
253
Propagation
254
Interfaces
255
256
5.7 Object Constraint Language (OCL)
257
OCL statements
258
An example: constraints on Polygons
259
260
Genealogy example: Possible solutions
261
262
System domain model vs System model
263
• The system domain model omits many classes that are needed
to build a complete system
—Can contain less than half the classes of the system.
—Should be developed to be used independently of
particular sets of
- user interface classes
- architectural classes
• The complete system model includes
—The system domain model
—User interface classes
—Architectural classes
—Utility classes
264
Suggested sequence of activities
• Identify a first set of candidate classes
• Add associations and attributes
• Find generalizations
• List the main responsibilities of each class
• Decide on specific operations
• Iterate over the entire process until the model is
satisfactory
—Add or delete classes, associations, attributes,
generalizations, responsibilities or operations
—Identify interfaces
—Apply design patterns (Chapter 6)
Don’t be too disorganized. Don’t be too rigid either.
265
Identifying classes
266
A simple technique for discovering domain
classes
• Look at a source material such as a description of
requirements
• Extract the nouns and noun phrases
• Eliminate nouns that:
—are redundant
—represent instances
—are vague or highly general
—not needed in the application
• Pay attention to classes in a domain model that represent types
of users or other actors
267
• Start with classes you think are most central and important
• Decide on the clear and obvious data it must contain and its
relationships to other classes.
• Work outwards towards the classes that are less important.
• Avoid adding many associations and attributes to a class
—A system is simpler if it manipulates less information
268
Tips about identifying and specifying valid
associations
• An association should exist if a class
- possesses
- controls
- is connected to
- is related to
- is a part of
- has as parts
- is a member of, or
- has as members
"" some other class in your model
• Specify the multiplicity at both ends
• Label it clearly.
269
270
Identifying attributes
271
272
An example (attributes and associations)
273
274
An example (generalization)
275
• To determine responsibilities
—Perform use case analysis
—Look for verbs and nouns describing actions in the system description
276
Categories of responsibilities
277
An example (responsibilities)
278
Prototyping a class diagram on paper
279
Identifying operations
280
An example (class collaboration)
281
282
Class collaboration ‘b’ 1
283
284
Class collaboration ‘d’
Changing the destination of a link
e.g. changing the Airplane of to a SpecificFlight, from
airplane1 to airplane2
1. (public) The instance of SpecificFlight
—deletes the link to airplane1
—makes a one-directional link to airplane2
—calls operation 2
—then calls operation 3.
2. (non-public) airplane1
—deletes its one-directional link to the instance of
SpecificFlight.
3. (non-public) airplane2
—makes a one-directional link to the instance of
SpecificFlight.
285
286
5.10 Implementing Class Diagrams in Java
• Attributes are implemented as instance variables
• Generalizations are implemented using extends
• Interfaces are implemented using implements
• Associations are normally implemented using instance variables
• Divide each two-way association into two one-way associations
—so each associated class has an instance variable.
• For a one-way association where the multiplicity at the other
end is ‘one’ or ‘optional’
—declare a variable of that class (a reference)
• For a one-way association where the multiplicity at the other
end is ‘many’:
—use a collection class implementing List, such as Vector
287
288