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.
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
Zugfolgeand 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.
Persistenzcould contain the methods
RestoreFromDatabaseand 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:
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.
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.
- Iain D. Craig: Object-Oriented Programming Languages: Interpretation . Springer Verlag, London 2007, ISBN 1-84628-773-1
- Gilad Bracha, William Cook: Mixin-based Inheritance . (PDF; 172 kB)
- Robert Eckstein: Mixins in JavaFX 1.2 Technology
- ↑ Ruby and Mixins: Website of the manufacturer
- ↑ Mixin in TclOO: Website of the manufacturer
- ↑ re-mix: mixin library for .NET languages on CodePlex: The open source re-mix library on CodePlex
- ↑ Example for Java with CGLIB: Multiple class inheritance in Java made easy