Introduction to UML
by K. Yue
1. Introduction to UML
- UML: A set of graphical notations for object oriented modeling.
- Wikipedia: "The Unified Modeling Language (UML) offers a way to visualize a system's architectural blueprints in a diagram."
- A standard maintained by OMG: OMG's UML page.
- Two major versions:
- Version 1.4.2: international standard released in 2005.
- Version 2.5.1: released in 2017, added nested classifiers and improved behavior models.
- Two main types of diagrams:
- Structure diagrams: static structures.
- Behavior diagrams: dynamic behaviors.
- Version 2.5 has 15 diagrams: 7 structure diagrams and 8 behavior diagrams.
- Some Resources:
- We will focus on the class diagram only.
Class Diagram of UML 2.2 diagram (from wikipedia):
2. Class Diagrams (Emphasis on DB applications)
Introduction:
- A static structure diagram in UML.
- "Describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the relationships among the classes." from Wikipedia.
- Read "class diagram" from Wikipedia: http://en.wikipedia.org/wiki/Class_diagram.
- For a significantly better introduction by IBM: http://www.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell/.
- Two kinds of tools for drawing UML diagrams:
- Graphical tools: main purposes are drawing (e.g. MS Visio, draw io, etc.)
- Computer-Aided Software Engineering (CASE) tools: for software development with some understanding of the semantics of diagram elements (e.g. MagicDraw, IBM Rational Rhapsody, Visual Paradigm, Astah, etc.)
- We will use Astah UML Editor
- We will use community version in classroom demonstration, which is now deprecated.
- Students can use the more powerful student version for free: search "astah student license".
- One may also use UML object diagrams to show objects and their associations of a snapshot of the system.
A Simple Conceptual Modeling Process
- Examine application requirements to gain a good understanding of the problem.
- Conduct an analysis to extract concepts that may have data requirements.
- For each concept, design how do model the concept. Options are:
- by attributes
- by a class
- by associations between classes
- no need to model
These steps are repeated until the model reaches the necessary fidelity, accuracy and precision.
Example:
Problem. An used car dealership application's subsystem: information about cars and their manufacturers.
Specification description: A car manufacturer has an unique id and name. A car maker may make many cars. For example, Honda, which may have an manufacturer id of 10001, makes Civic and Accord.
Analysis and Design
- Manufacturer: a class (template) that can be used to initiate many manufacturer objects (instances).
- Honda: an object of the class Manufacturer.
- Resolve ambiguous terms: manufacturer, may refer to the manufacturer class or a particular manufacturer (e.g. Honda).
- Define synonym: manufacturer, car manufacturer, car maker. Different terms can refer to the same concept.
- Unique id: attribute (name), a property of the manufacturer class.
- Make additional assumptions: Every manufacturer object must have an unique id.
- 10001: attribute (value) of a manufacturer object.
- Name: a property of manufacturer.
- Additional assumption: Every manufacturer object must have a name.
- Car: a class as there may be many brands of cars.
- Prepare questions: Do we need to introduce the concept Model (e.g. Coupe, Sedan, Si Coupe)?
- Civic and Accord: object instance of Car.
- Additional assumption: Every car must have a name as its attribute.
- Make, or manufacture: a relationship between a manufacturer (object) and a car (object).
Class Diagram:
Object Diagram:
Classes:
- Drawn as a rectangular box.
- The class names, attributes, and operations may be specified, with selected details in the name, attribute and operation compartments respectively.
- Attribute and operation compartments are optional.
- For DB modeling, the attribute compartment will eventually need to be clearly modeled. The operation compartments can be missed.
- The levels of details depend on the phase of modeling. It is a common mistake to specify too much detail in the early modeling phases.
- As modeling proceeds, more details are added, updated, and refined.
Note that software application modeling and database modeling has different focus.
- Application modeling: focus on operations (methods).
- Database modeling: focus on attributes (data).
Example: The following diagram show the Window class in three phases of modeling. Note that this is for software modeling, not data modeling.
- For example, one may focus on the main classes and their associations in the first model, without worrying about the attribute or operation compartments.
- Most UML editors allow controlling visibility of different elements. For example, in Astah:
- A stereotype (specifying the kind of entities) and a property list with tagged values can be added to any compartment.
- Their flexibility allows for customization and extensibility to fit specific applications.
- Additional properties on data members may be specified, such as:
- Visibility: + (public: +, protected: #, private: -, etc.)
- data types.
- abstract (in italic) or concrete (as constraints)
- class members (underscored) or instance members
- default values
Example: for software modeling:
Example: for database modeling.
The class Patron may be identified in the first draft of the UML class diagram.
- In a subsequent iteration, attributes may be added using settings of the UML tool showing visibility of the attribute members.
- Data types may be included using predefined data types provided by the tool.
- In a further iteration, stereotype may be added, such as to identify the primary key <<PK>> and simple candidate key <<unique>>.
- More specific user-defined types (or implementation types) may be used.
- Operation members may be added. They are in general much less important than data members in database modeling.
- Multiplicity should eventually be added, as shown in the diagram below.
- Note that multiplicity can be used to depict nullability and multi-valued attributes. In this example, Phone is nullable and Hobbies can have multiple values.
Examples:
Class diagrams from agile modeling and wikipedia.
- Some possible relational database options/extensions on attributes may include:
- Multi-valued: * or by using multiplicity.
- Multiplicity can also be used to indicate whether an attribute is nullable.
- Derived: <<derived>>, \, or using other specific notation
- Primary key: <<PK>> as stereotype.
- Candidate keys: <<CK>> as stereotyp
- Unique field: <<unique>> as stereotype.
- Nullability: <<nullable>> or by using multiplicity.
- User or system defined SQL data types.
- Indexing: <<index>> as stereotype.
- Check with your organizations for UML guidelines on a specific project.
- An example of a database profile for UML: http://www.agiledata.org/essays/umlDataModelingProfile.html
- may be adapted for uses in later phase of modeling.
- A class is a 'first class citizen'.
- It has attributes
- It can form an associations with other classes.
- Objects of a class can exist by themselves.
- As a comparison, an attribute is not a first class citizen.
- It does not have sub-attributes
- It cannot form associations.
- The existence of an attribute depend on the object.
- A class in a class diagram have some properties of a data type.
- Objects can be instantiated from classes.
Example:
We may have four objects of the student class: S1, S2, S3 and S4. Each student object represents an individual student in a database application.
We may have three objects of the course class: C1, C2, and C3. Each course object represents an individual course in the database application.
Associations
- Binary associations are represented by solid lines.
- Important options include:
- Association names with reading direction arrows.
- Association roles.
- Multiplicities.
- Association attributes, usually stored as association classes.
- Qualifiers: association attributes to partition the targeted classes.
- Navigational requirement: specified by arrows.
- Dependency constraints: by dotted lines.
- Some modeling questions/decisions:
- Should we model something as a class or as an association?
- Should we model something as a class or an attribute?
- What kind of association should I use? Binary association, association class, n-ary association?
Example: Note that no attributes are shown in this initial phase.
Note:
- Job is an association class.
- The arrow in the association "works-for" shows the direction of the association.
- The association "manages" is between two job objects.
- The {or} designation specifies the partition of the account class into two classes: person (account) and corporate (account).
Example:For:
The association Enroll describes the association 'type'. The association is actually between two objects (a student object and a course object). Examples:
S1 -- C1: meaning student S1 is enrolled in course C1.
S1 -- C3
S2 -- C1
S2 -- C2
S2 -- C4
S3 -- C3
S4 -- C1
S4 -- C4
- Multiplicity can be specified by a number, the symbol * (many), a range, or a set.
- Multiplicity is a very common source of errors. Please refer to the explanation of the following diagram until you are very clear about it.
- Meaning:
- Every X object is associated with n Y objects.
- Every Y object is associated with m X objects.
Example
What do you think about these class diagrams?
(a)
(b)
(c)
- Aggregation indicator (hollow diamond) and composition indicator (solid diamond):
- Aggregation models the ‘a-part-of’ relationship. E.g. car-wheel.
- Composition is a strong form of aggregation: the part's life cycle is dependent on the wholes life-cycle; e.g. university-department, building-room.
- They can also be represented by using multiplicity.
Example: Aggregation and Composition
What do you think about this composition and aggregation examples in: http://en.wikipedia.org/wiki/File:Congregationalism?
- N-ary associations are represented using a diamond connecting to participating classes.
- Not so common.
- May be modeled as a class instead.
A not so good example from a tutorial:
Tips:
- In modeling, a tertiary association can be replaced by a number of binary associations.
- Don't use n-ary associations where n>=3 unless you are sure.
- Generalization is represented a hollow triangle at the superclass.
- Generalization models the 'a-kind-of’ association.
- Avoid multiple copies of member definition.
- Some options of generalization includes:
- discriminator (the name of the partition),
- powertype (a class in which an instance of it is a subclass of the superclass),
- constraints (overlapping, disjoint, complete, incomplete and user defined constraints).
Example:
- There are many possible options and extensions.
Constructing class diagrams: some tips
- There are many methodologies and best practice tips to construct effective class diagrams.
- There are many possible modeling options: e.g. classes versus attributes, classes versus associations, multiplicity, etc.
- Need to fully understand the assumptions and implications when making modeling decisions.
- Do not model implementation details in earlier modeling phases.
Classwork We will work on a problem from a previous semester.