• 8 heures
  • Moyenne

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 05/05/2022

Improve Your Class Diagram

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  seasonattribute to theProductionclass?

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: 

Inheritance model
Inheritance model

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:

Class diagram in full
Class diagram in full

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 of  Production  . Similarly, Mrs. Doubtfire is an instance of  FeatureFilm  . 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:

Defining Production and Series as abstract classes
Defining  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:

Model showing aggregation
Model showing aggregation

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:

Model showing a ternary association
Model showing 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:

Creating a Course class to avoid a ternary association
Creating a  Course  class to avoid a ternary association

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. 

Diagram showing a note attached to the Shooting class
Diagram showing a note attached to the  Shooting  class

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:

Model showing two associations using two lines named “writes” and “translates”
Model showing two associations using two lines named “writes” and “translates”

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”:

Model showing class roles
Model showing class roles

Reflexive Association

A class can link to itself!

For example, a  Category  class could have child and parent categories: 

Diagram showing child-categories
Diagram showing child-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.

Exemple de certificat de réussite
Exemple de certificat de réussite