Mixins first appeared in the Symbolics' object-oriented Flavors system, which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts: The ice cream shop owner offered a basic flavor of ice cream (vanilla, chocolate, etc.) and blended in a combination of extra items (nuts, cookies, fudge, etc.) and called the item a "Mix-in", his own trademarked term at the time.
Mixins encourage code reuse and avoid well-known pathologies associated with multiple inheritance. However, mixins introduce their own set of compromises.
A mixin can also be viewed as an interface with implemented methods. When a class includes a mixin, the class implements the interface and includes, not inherits, all the mixin's attributes and methods. They become part of the class during compilation. Interestingly enough, mixins don't need to implement an interface. The advantage of implementing an interface is that instances of the class may be passed as parameters to methods requiring that interface.
A mixin can defer definition and binding of methods until runtime, though attributes and instantiation parameters are still defined at compile time. This differs from the most widely-used approach, which originated in the programming language Simula, of defining all attributes, methods and initialization at compile time.
In Simula, classes are defined in a block in which attributes, methods and class initialization are all defined together; thus all the methods that can be invoked on a class are defined together, and the definition of the class is complete.
With mixins the class definition defines only the attributes and parameters associated with that class; methods are left to be defined elsewhere, as in Flavors and CLOS, and are called "generic functions". These generic functions are functions which are defined in multiple cases by type dispatch.
Other than Flavors and CLOS, some languages that use mixins are:
SocketServermodule has both a
TCPServerclass that act as a server for UDP and TCP socket servers. Normally, all new connections are handled within the same process. Additionally, there are two mixin classes:
ThreadingMixIn. By extending
ThreadingMixInlike this the
ThreadingMixInclass adds functionality to the TCP server such that each new connection creates a new thread. Alternatively, using the
ForkingMixInwould cause the process to be forked for each new connection. Clearly, the functionality to create a new thread or fork a process is not terribly useful as a stand-alone class.
In this usage example, the mixins provide alternative underlying functionality without affecting the functionality as a socket server.
Interfaces combined with aspect-oriented programming can produce full fledged mixins in languages that support such features, such as C# or Java. Aditionally, through the use of the marker interface pattern, generic programming, and extension methods, C# 3.0 has the ability to transparently mimic mixins.