Skip to main content

How to enable polymorphism for generalization?

On polymorphism​

In some cases, there might be a need to define a general concept with various specific interpretations. Some examples:

  • In Domain Driven Design, three DomainObjects can be defined (Value Object, Entity, Service).
  • A task is characterized by its processing, which can be asynchronous or synchronous.
  • A microserver can contain communication nodes to external parties, with a certain protocol (MQTT, REST)

Here, it might be interesting to define general ElementClasses to capture the common properties, while creating instantiable ElementClasses for the specific entities.

This concept of providing a general interface for different entities is called polymorphism.


Modelling superclasses​

In our metamodel tooling, ElementClasses can define a superclass. Instances of one of these ElementClasses can then also be used as an instance of the SuperClass. This way, we support polymorphism in the models.

Modelling steps:

  1. Create the superclass (the general concept). Mark it isAbstract, so it cannot be instantiated directly, but only through its subclasses. See isAbstract for the exact contract, and isSealed if you want to close off the set of subclasses to your own ontology.
  2. Define the subclasses (the specific entities). Set superClass on each. The subclass inherits all Attributes and References of the superclass, see superClass for details.
Polymorphism, not inheritance

This setup looks like standard object-oriented inheritance, but the intent is different: it exists strictly to support polymorphism, not deep hierarchical implementation reuse. Class inheritance introduces tight coupling and cascading changes down the tree. Keep generalization hierarchies flat, ideally one level deep (Abstract Base → Concrete Implementation).

ValueType.xml
<elementClass name="ValueType">
<isAbstract>true</isAbstract>
<isSealed>false</isSealed>
<group name="valueTypes" ontology="elements"/>
<role name="data"/>
<attributes>
<attribute name="name">
<isIdentifier>true</isIdentifier>
<dataType name="Name" ontology="elements"/>
</attribute>
<attribute name="description">
<isIdentifier>false</isIdentifier>
<dataType name="Description" ontology="elements"/>
</attribute>
</attributes>
</elementClass>