Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 39 ยท Deployment and Packaging

Profiles and Stereotypes

Learn how UML extends itself: stereotypes, tagged values, constraints and profiles, with examples such as a REST profile and a runnable stereotype validator.

UML defines classes, components, nodes and a few dozen other elements. Your project, however, talks about REST controllers, database tables, domain events and microservices. You could model them all as plain classes and lose the meaning, or you could extend UML so the model uses your vocabulary. UML provides a lightweight extension mechanism for exactly this: stereotypes, tagged values and profiles.

You already used stereotypes: <<interface>>, <<enumeration>>, <<component>> and <<artifact>> are built-in ones. In this lesson you learn to define your own and to package them.

Stereotypes

A stereotype gives a model element an additional, more specific meaning. It is written in guillemets above the element name, <<service>> OrderService, or shown with a custom icon. A stereotype always extends a metaclass, that is, a kind of UML element: you can say "<<entity>> extends Class" or "<<queue>> extends Node". It cannot be attached to an element of another metaclass.

Why bother?

  • Communication: readers see the intent immediately.
  • Constraints: you can attach rules ("an <<entity>> must have an id attribute").
  • Tools: code generators use stereotypes to pick templates (<<entity>> produces a JPA class, <<controller>> a REST endpoint).

A stereotype is not a subclass. It does not create a new class in your system; it labels an element in the model.

Tagged values

A stereotype can define tagged values (also called tags or stereotype properties): named properties that hold data. They are written in braces next to the stereotype or in a compartment.

TEXT
<<entity>>
Customer
{table = "customers", cache = true}

Typical tags: table, version, owner, latency = 200ms, idempotent = true. A tag has a type, a default value and may be optional. Think of tags as annotations: Java's @Entity(name="customers") is exactly a stereotype with a tag.

Constraints

A constraint is a rule in braces that must always hold for the element, often in OCL or natural language: , , , {self.balance >= 0}. Stereotypes commonly come with constraints, such as "every <<entity>> has exactly one attribute with the tag ".

Profiles

A profile is a package of stereotypes (plus the tagged values and constraints that go with them) that adapts UML to a platform or a domain. It is applied to a model with the <<apply>> relationship, so the model gains the new vocabulary without changing UML itself.

Well-known profiles show the range:

Profile Purpose
SysML Systems engineering: blocks, requirements, parametric diagrams
MARTE Real-time and embedded systems modeling and analysis
UML Profile for Java/EJB, JPA Mapping to Java platforms
Your own "Microservices", "Event Sourcing", "Hexagonal"

Writing an in-house profile is a good investment when several teams model the same kind of system and want consistent words.

classDiagram
    class Entity {
        <<stereotype>>
        +String table
        +Boolean cached
    }
    class Service {
        <<stereotype>>
        +Boolean idempotent
    }
    class UmlClass {
        <<metaclass>>
    }
    UmlClass <|-- Entity : extends
    UmlClass <|-- Service : extends

The profile "Shop" defines two stereotypes that extend the metaclass Class. Both are defined once and then used in many diagrams.

Defining a lightweight profile in practice

Not every team needs a tool that supports profiles formally. A common, practical approach:

  1. List the 5 to 10 kinds of elements your architecture really has.
  2. Give each a stereotype name and the UML metaclass it extends.
  3. Define its tags and rules in a short table in the team wiki.
  4. Use the stereotypes consistently in diagrams, and add them to the legend of every diagram.

Keep stereotypes few. A model where every class has its own stereotype carries no information.

Mermaid and text tools

Mermaid supports annotations in class diagrams (<<interface>>, <<service>>), so stereotype labels work. Tagged values can be written as members. PlantUML and most UML tools support full stereotypes, with tag compartments and custom icons.

Validating stereotypes with code

If stereotypes carry rules, the rules can be checked. This model lists elements, their stereotypes and tags; the validator checks the profile constraints.

Python Validate a model against a profile

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stereotypes that drive code generation

A stereotype can select how code is generated. The next sketch turns stereotyped classes into Python source, using tags to configure the output.

Python Generate code from stereotypes and tags

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real generators work the same way: the stereotype selects the template, the tags fill the gaps. From Models to Code develops this idea.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a stereotype?

    Stereotypes classify elements in the model. They are not classes of your running system.
  2. 2. What is a tagged value?

    Tags are the properties of a stereotype, similar to annotation parameters.
  3. 3. What is a profile?

    SysML and MARTE are profiles, and teams can define their own.
  4. 4. Which is good profile practice?

    A small, documented vocabulary carries information; a stereotype may only extend the metaclasses it declares.

Summary

  • Stereotypes extend UML metaclasses to give elements domain or platform meaning.
  • Tagged values hold data on a stereotype; constraints state rules that must hold.
  • A profile bundles stereotypes, tags and constraints and is applied to a model.
  • Keep profiles small, documented and consistent; check the rules automatically.
  • Generators use stereotypes and tags to select templates and fill them.

Next: Timing and Interaction Overview Diagrams