# Mixin

In object-oriented programming, a mixin is an associated, reusable bundle of functionality that can be added to a class . Mixins are directly supported by some programming languages , for example Ruby , Scala , Tool Command Language or Go . In other languages, this design pattern is used, for example, by means of mixin classes in connection with multiple inheritance or, e.g. B. in JavaScript , implemented by means of generic programming .

Unlike interfaces (interface) , such as those in Java and C # are common, a mixin includes but typically already functionality that can be used.

A trait is a special form of a mixin with a more flexible integration into classes.

## example

Modeling a chess game ( UML ) and mixin for persistence

The following example illustrates a use case of the mixin design pattern. It contains a class hierarchy that models a chess game . In this model it is appropriate for the classes `Zugfolge`and to be able to make them persistent . This means that objects of these classes  can be stored in a non-volatile storage medium - for example a database - and reconstructed from this again. For other classes of this model, however, it does not make sense to support persistence. `Spielstellung`

The mixin `Persistenz`could contain the methods `StoreInDatabase`and `RestoreFromDatabase`and by means of them save an object of the classes in which this mixin is integrated in the database or reconstruct it from it.

## Mixins and Inheritance

While in languages ​​like C ++ and Eiffel mixins are mapped as classes using multiple inheritance , there is also the view of a mixin-based inheritance as a generalization of the inheritance mechanism implemented in classic object-oriented languages ​​such as Smalltalk or Simula . If one considers inheritance as an incremental mechanism that modifies a base class (B), the result (E) is some kind of combination of the base class with a modification (M). This can be formulated as follows:

${\ displaystyle E = B \, \ triangle \, M = B + M (B)}$

With inheritance in conventional form, the modification (M) has no independent existence, it is part of the result (E). In contrast to this, with mixin-based inheritance one starts from the point of view that the modification is an abstraction that is independent of the base class (B) and the result. This modification is the mixin, the symbol stands for the mixin application , i.e. the use of the mixin. ${\ displaystyle \ triangle}$

## Programming languages ​​that use mixins

In some programming languages, mixins are part of the programming language itself. Examples of this are the programming languages Ruby and Tool Command Language . The mixin functionality can partly be implemented in other programming languages ​​with external libraries (for example the languages ​​of the Microsoft .NET family or Java ).

An extensive list of programming languages ​​that support mixins can be found in the Wikipedia article on mixins.

## literature

• Iain D. Craig: Object-Oriented Programming Languages: Interpretation . Springer Verlag, London 2007, ISBN 1-84628-773-1