Contents
Models
What are models?
Representation of a system in the form of notations or a language
Modelling - the process of representing the system’s main behaviour, characteristics and states using a notation or language (Low-level: Java; High-level: UML).
Purpose of models?
- Show essential details
- Omit irrelevant details
- Show the communication of relevant information
- Demonstrate a form of Abstraction
Depending on different needs, we can have different levels of detail for a model.
Abstraction & Reification
Abstraction
The process of removing unnecessary details… making the model more abstract
Reification
The process of adding information, or implementing the model to make it a concrete, working model. This requires iterating through a number of intermediate models that are becoming more concrete, or building a system by progressively adding more details.
Diagrams
A Diagram is a partial representation of a model, depicting a part of the entire system. Usually need many types of diagrams to completely represent a system.
Can manifest a variety of aspects:
- Structural elements: Class, Interface, Collaborations, Use case, Component.
- Behavioural elements: Interaction (overview), State machine
- Grouping elements: Package, Subsystem
- Extension mechanisms: Profiles, stereotypes, tagged values
- etc.
Next we look at a subset of them in somewhat further detail.
Structural Elements
Class Diagram*
Object Diagram
Use Case Diagram
Component Diagram
Deployment Diagram
Behavioural Elements
Interaction Diagram
Sequence Diagram
State Machine Diagram
Activity Diagram
Relationships
Relationships between classes are represented as links between class boxes in UML, and are realised by object references in Java.
Association
In standard UML, associations are, by default, bi-directional, as represented by these single links without arrows. Adding arrows makes it uni-directional.
Multiplicity
The term multiplicity 多重性 refers to the numberings labelled on the link:
In this case, the association is Has-Capital, and means that one and only one Country has the association Has-Capital with one and only one City, and vice versa (bi-direction).
If the numbering is replaced with something like 1…* or 0…*, this only means that a class has this association with multiple copies of another class. This can be implemented as a method returning an array of objects, or a method taking in multiple objects as argument. Therefore, this is not to be confused with Aggregation, which can only be represented as an instance variable holding an array of the other objects.
Aggregation
Aggregation means directly that an object holds multiple references to other objects as an attribute. Thus it should be implemented as a private instance variable.
In UML, Aggregation is given by the diamond symbol on the link.
Java Implementation
Although associations are typically defined as bi-directional, in Java, however, there is no way of enforcing such bi-directional connection, as the link can only be maintained by a uni-directional reference call. So remember to use the arrowed representation.
Returning null references
In implementations of associations, you will have to write methods that are not in the type void
, which means that you will have to return a reference to an object, even when such object is not found. In this case you return an null
reference, which seems natural.
However, on the client side, you must not forget this possible outcome, so after you call a getter method to have an object reference returned, you MUST NOT hurry on to carry out further operations on that reference. Instead, you MUST first check that the reference is not null
, because operations on a null
reference raises a NullPointerException
, which crashes your client program!