Mixin

As a mixin a matched, reusable set of functionality is called in object-oriented programming, that can be added to a class. From some programming languages ​​mixins are directly supported, such as Ruby, Scala, or Go. In other languages ​​this design pattern is dependent on the capabilities of the programming example using mixin classes in conjunction with multiple inheritance or, as for example implemented in JavaScript using generic programming.

A trait is a particular form of mixins with a more flexible integration into classes.

Example

The following example illustrates an application of mixin design pattern dar. It contains a class hierarchy that models a chess game. In this model for the classes sequence of moves and game plans attached to be able to make this persistent. This means that objects of this class in a non- volatile storage medium - for example, a data - stored, and from this can be reconstructed. For other classes of this model, however, the support of persistence is not useful.

The Mixin ' persistence could the methods StoreInDatabase and Restore From Database included and this means an object of the class in which this mixin is included, store it in the database or reconstructed from these. Unlike ports ( interface), such as are customary, for example, Java and C #, but typically includes a mixin this functionality can be used.

Mixins and inheritance

While displayed in languages ​​such as C and Eiffel mixins as classes using multiple inheritance, there is also the view to see a mixin - based inheritance as a generalization of the implemented in classical object-oriented languages ​​such as Smalltalk or Simula inheritance mechanism. If you look at inheritance as incremental mechanism that modifies a base class (B ), the result ( E) is a kind of combination of the base class with a modification (M). This can be formulated as follows:

In inheritance in conventional form, the modification ( M) has no independent existence, it is part of the result ( E). In contrast, one goes out to the mixin - based inheritance from the standpoint 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, that the application of mixins.

Programming languages ​​that use mixins

In some programming languages ​​mixins are part of the programming language itself An example of this is the Ruby programming language. In some cases, the mixin functionality can be implemented in other programming languages ​​with external libraries (such as the languages ​​of the Microsoft - .NET - family).

576561
de