At the moment, you understand the basics of UML modeling (how to represent and link classes). Now it’s time to refine your model.
Generalize and Specialize Your Classes
We’ve discussed films generically; however, your data is more specific than that. A film might be a feature film, TV film, or series (web or television). To be more accurate, let’s use the term “production” instead of film.
Up to now, you have used the attribute filmingType
to store the type of production.
I’ve noticed in the CSV file that a series sometimes shows the season number. Can we add season
attribute to theProduction
class?
Good idea.
However, this attribute only applies if the production is a series. It will remain blank if the production is a TV or feature film. This solution isn't ideal. It would be better to place this attribute into aSeries
class.
Thankfully, this is possible using inheritance as follows:
The inheritance relationship shows a white (hollow) triangular arrowhead indicating that the Production
class (the parent class) is more general than its more specialized child classes.
So, all classes ( TvFilm
, Series
, and FeatureFilm
) automatically inherit attributes and methods from the Production
class. By doing this, all of the child classes will inherit two attributes ( title
, releaseYear
) and one method ( determineFilmDecade()
). The Series
class will have three attributes: title
, releaseYear
, and season
and one method ( determineFilmDecade()
).
You could even go crazy and create another level of inheritance: TVSeries
and WebSeries
! Here’s the result:
Control Instantiation of Classes Using Abstract Classes
When you use inheritance, it’s sometimes helpful to specify that a class is abstract (i.e., it cannot be instantiated).
In your database, you want a single production to be a TV film, a TV series, a web series, or a feature film. Calling something a production is too general. For example, if you considered a TV film to be simply a production, you would lose some of the specific details.
For example, CSI: NY is an instance of
TVSeries
but is not an instance ofProduction
. Similarly, Mrs. Doubtfire is an instance ofFeatureFilm
. So, you don’t want to store a production in your database without specifying whether it’s a TV film, a series, or a feature film.
If you want to denote an abstract class, put the title in italics. Here’s what it looks like if you define Production
and Series
as abstract classes:
Discover Further UML Features
Here are some other options for linking classes. We won't go into detail on all of them, but it's good to know these options exist. I’ll give you some useful links if you’d like to explore the topic in more detail.
Aggregation
Use an aggregation when a class is a collection or grouping of objects. It’s very similar to a standard association, and whether or not you use it, this won’t make a difference when you translate your CDM into an LDM.
It’s denoted as follows, using an empty diamond shape:
Unlike composition, there is no constraint around multiplicities or the lifecycle of the objects in the aggregation relationship. They can exist even when the object they’re aggregated with disappears.
Ternary Association
It’s possible to create associations between more than two classes. These are N-ary associations. Here’s an example of a ternary association:
In practice, you never use more than three associations. However, you can always convert ternary associations into three binary associations by converting the association itself into a new class like below:
Notes
You can add notes to class diagrams adding details such as attribute constraints.
Suppose you also had an endDate
attribute for the Shooting
class. You could specify a constraint on your diagram saying that startDate
must be a date before endDate
. While this may appear obvious, you need to specify this within your RDBMS if such an attribute were available because it won’t happen automatically.
You can also use notes to provide further detail about attribute domains when they are more complex than Integer
, Date
, String
, etc.
For example: “Integer between 1000 and 2000.”
Double Associations
You can link two classes via two (or more) associations.
For example, use this association if someone wrote a book, and then another person translated it into another language:
Class Roles
Rather than naming the associations, you can also indicate an association using the role that each class undertakes. For example, look at this example, where we replaced the name of the association “drive” with “driver” and “mode of transport”:
Reflexive Association
A class can link to itself!
For example, a Category
class could have child and parent categories:
Further Information About UML
If you’d like to take your UML knowledge a step further, here is a helpful link:
Let’s Recap!
A generalization association is modeled using inheritance, which allows an entity to be general or specialized.
Inheritance is sometimes used with abstract classes that cannot be instantiated.
There are other types of association, such as aggregation and ternary.
Use notes to add detail about constraints or any other comments.
You can link two classes by more than one association, each having a different name or role. A class can also link to itself using a reflexive association.
So, there you have it! You know (almost) all there is to know about UML class diagrams. In the next chapter, you’re going to put down your pencil and learn how to create a class diagram using dedicated software.