Global training solutions for engineers creating the world's electronics products

Mix-In Classes

The purpose of a mix-in is to add some functionality in to the class hierarchy at one of its nodes. The functionality of the mixed-in class is shared by the class and its subhierarchy and the same mixed-in class can be added at distinct nodes to ensure that only the desired class sub-hierarchies have the specific functionality. The aim is avoid distorting the class hierarchy by having the mixed-in functionality appear as a common ancestor of classes which otherwise have nothing to do with each other.

It is possible for more than one class to be mixed-in at any point and for the mixed-in class to appear not only at different points within the same class hierarchy, but also in unrelated hierarchies.

In SystemVerilog, a mix-in can be declared by type parameterising a class with the class it extends from:

     class Component_Reporter #(type P = Reporter) extends P;
     ...
     endclass
     

Here, the functionality provided by the parameter class, P, is accessible to the Component class and its class sub-hierarchy.