Generator Gap pattern / Extension Classes / 3 level inheritance

A common problem when using code generation arises when you need to expand the functionality of the generated code with your own hand written code.

Since code generators, such as TRIMM, re-generate code every time that either the model (e.g. a UML model) or the code generators configuration changes. This means that if you manually edit code generation files, then these changes will be lost next time the code generators is run (that is unless the code generator support protected regions, which is something we don’t recommend using unless for VERY simple use cases).

Each programming languages has different ways of allowing you to generate code and still provide your own extensions to the generated code. In C# you can for instance use partial classes. In Java you have to either resort to Privileged Aspects from Aspect Oriented Programming (AOP), custom homemade MixIns/Traits (which are cumbersome in traditional Java) or use inheritance.

The Generator Gap pattern is based on inheritance. It’s also know as Extension Classes in TRIMM (as this TRIMM feature predates the coining of the Generator Gap pattern name). Others refer to this pattern as 3 level inheritance.

The pattern is visualized in the picture below:

Generator Gap Pattern/Extension Classes/3 level inheritance explained

Generator Gap Pattern/Extension Classes/3 level inheritance explained

In the example we have modeled two classes A and B which have an association to each other.

When you apply the TRIMM Extension Class generator pattern to such as model the resulting model will be two abstract classes called AbstractA and AbstractB. These classes contain all the details of the modeled A and B such as properties as well as EventListener specific code, such as JPA annotation, History, Bi-directional code, etc.
The AbstractA and AbstractB classes are generated every time the code generates runs.

A concrete class for the modeled classes A and B. These are only Generated once if they don’t already exist.
You are then free to add your own custom code to the concreate classes A and B.

The reason for the name 3 level inheritance is that some codegenerators, here among TRIMM, supports placing in another hand written (Abstract) Class at the root of the inheritance hierarchy. TRIMM will ensure that any hierarchy only inherits once from this root Class. This is exemplified in the picture below:

3 level inheritance example

3 level inheritance example

< Back