Page namespace (page_namespace ) | 0 |
Page title without namespace (page_title ) | 'Mixin' |
Full page title (page_prefixedtitle ) | 'Mixin' |
Old page wikitext, before the edit (old_wikitext ) | 'In [[object-oriented programming language]]s, a '''mixin''' is a [[class (computer science)|class]] that provides a certain functionality to be [[Inheritance (computer science)|inherited]] by a subclass, but is not meant to stand alone. Inheriting from a mixin is not a form of specialization but is rather a means to collect functionality. A class may inherit most or all of its functionality by inheriting from one or more mixins through [[multiple inheritance]].
Mixins first appeared in the Symbolics' object-oriented [[Flavors (computer science)|Flavors]] system, which was an approach to object-orientation used in [[Lisp Machine Lisp]]. The name was inspired by [[Steve's Ice Cream|Steve's Ice Cream Parlor]] in [[Somerville, Massachusetts]]:<ref>[http://www.linuxjournal.com/article/4540 Using Mix-ins with Python<!-- Bot generated title -->]</ref> 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.<ref>[http://listserv.linguistlist.org/cgi-bin/wa?A2=ind0208a&L=ads-l&P=11751 LISTSERV 14.4<!-- Bot generated title -->]</ref>
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, rather than 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.
==Definition and implementation==
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 function]]s". 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:
*[[C++]]{{Fact|date=March 2009}}
*[[ColdFusion]] (Class based using includes and Object based by assigning methods from one object to another at runtime)
*[[D programming language|D]] (called [http://www.digitalmars.com/d/template-mixin.html "template mixins"])
*[[Factor programming language|Factor]]{{Fact|date=March 2009}}
*[[Perl]]{{Fact|date=March 2009}}
*[[PLT Scheme]] ([http://docs.plt-scheme.org/guide/classes.html#(part._.Mixins) mixins documentation])
*[[Python (programming language)|Python]]
*[[Ruby programming language|Ruby]]
*[[Scala (programming language)|Scala]]
*[[Smalltalk]]
*[[Strongtalk]]
*[[XOTcl]] (an object system for [[Tool Command Language|Tcl]])<ref>[http://media.wu-wien.ac.at/doc/tutorial.html#mixins Mixin classes in XOTcl]</ref>
Some languages like [[ECMAScript]] (commonly referred to as JavaScript) do not support mixins on the language level, but can easily mimic them by copying methods from one object to another at runtime, thereby "borrowing" the mixin's methods. Note that this is not possible with [[static typing|statically typed]] languages, where an object's signature is fixed at compile time.
==Example==
In [[Python (programming language)|Python]], the <code>SocketServer</code> module has both a <code>UDPServer</code> and <code>TCPServer</code> class that act as a server for [[User Datagram Protocol|UDP]] and [[Transmission Control Protocol|TCP]] socket servers.
Normally, all new connections are handled within the same process.
Additionally, there are two mixin classes: <code>ForkingMixIn</code> and <code>ThreadingMixIn</code>.
By extending <code>TCPServer</code> with the <code>ThreadingMixIn</code> like this
<source lang="python">
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass
</source>
the <code>ThreadingMixIn</code> class adds functionality to the TCP server such that each new connection creates a new [[thread (computer science)|thread]].
Alternatively, using the <code>ForkingMixIn</code> would cause the process to be [[fork (operating system)|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.
==Commentary==
Some of the functionality of mixins is provided by [[interface (computer science)|interfaces]] in popular languages like Java and C#. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for refactoring common behavior into a single place.
Interfaces combined with [[aspect-oriented programming]] can produce full fledged mixins in languages that support such features, such as C# or Java. Additionally, through the use of the [[marker interface pattern]], [[generic programming]], and extension methods, C# 3.0 has the ability to mimic mixins.{{Fact|date=January 2009}}
==See also==
* [[abstract type]]
* [[trait (abstract type)]]
* [[Policy-based design]]
==References==
<references />
== External links ==
* [http://c2.com/cgi/wiki?MixIn Wiki entry] at Portland Pattern Repository
* [http://www.waysys.com/ws_content_bl_fuood_ch13.html Mix-in Classes chapter] from Fundamentals of Object-Oriented Design book by Meilir Page-Jones.
* [http://www.macromedia.com/support/documentation/en/flex/1/mixin/index.html Mixins] in [[ActionScript]].
* [http://scala.epfl.ch/intro/mixin.html Scala Overview: Mixin Class Composition] - a step-by-step example in [[Scala programming language|Scala]]
* [http://www.dreamsongs.com/NewFiles/ECOOP.pdf The Common Lisp Object System: An Overview] by [[Richard P. Gabriel]] and Linda DeMichiel provides a good introduction to the motivation for defining classes by means of generic functions.
[[Category:Object-oriented programming languages]]
[[de:Mixin]]
[[es:Mixin]]
[[fr:Mixin]]
[[nl:Mixin]]
[[ja:Mixin]]
[[ru:Примесь (программирование)]]' |
New page wikitext, after the edit (new_wikitext ) | ''''Jason was here'''''Italic text''
Mixins first appeared in the Symbolics' object-oriented [[Flavors (computer science)|Flavors]] system, which was an approach to object-orientation used in [[Lisp Machine Lisp]]. The name was inspired by [[Steve's Ice Cream|Steve's Ice Cream Parlor]] in [[Somerville, Massachusetts]]:<ref>[http://www.linuxjournal.com/article/4540 Using Mix-ins with Python<!-- Bot generated title -->]</ref> 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.<ref>[http://listserv.linguistlist.org/cgi-bin/wa?A2=ind0208a&L=ads-l&P=11751 LISTSERV 14.4<!-- Bot generated title -->]</ref>
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, rather than 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.
==Definition and implementation==
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 function]]s". 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:
*[[C++]]{{Fact|date=March 2009}}
*[[ColdFusion]] (Class based using includes and Object based by assigning methods from one object to another at runtime)
*[[D programming language|D]] (called [http://www.digitalmars.com/d/template-mixin.html "template mixins"])
*[[Factor programming language|Factor]]{{Fact|date=March 2009}}
*[[Perl]]{{Fact|date=March 2009}}
*[[PLT Scheme]] ([http://docs.plt-scheme.org/guide/classes.html#(part._.Mixins) mixins documentation])
*[[Python (programming language)|Python]]
*[[Ruby programming language|Ruby]]
*[[Scala (programming language)|Scala]]
*[[Smalltalk]]
*[[Strongtalk]]
*[[XOTcl]] (an object system for [[Tool Command Language|Tcl]])<ref>[http://media.wu-wien.ac.at/doc/tutorial.html#mixins Mixin classes in XOTcl]</ref>
Some languages like [[ECMAScript]] (commonly referred to as JavaScript) do not support mixins on the language level, but can easily mimic them by copying methods from one object to another at runtime, thereby "borrowing" the mixin's methods. Note that this is not possible with [[static typing|statically typed]] languages, where an object's signature is fixed at compile time.
==Example==
In [[Python (programming language)|Python]], the <code>SocketServer</code> module has both a <code>UDPServer</code> and <code>TCPServer</code> class that act as a server for [[User Datagram Protocol|UDP]] and [[Transmission Control Protocol|TCP]] socket servers.
Normally, all new connections are handled within the same process.
Additionally, there are two mixin classes: <code>ForkingMixIn</code> and <code>ThreadingMixIn</code>.
By extending <code>TCPServer</code> with the <code>ThreadingMixIn</code> like this
<source lang="python">
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass
</source>
the <code>ThreadingMixIn</code> class adds functionality to the TCP server such that each new connection creates a new [[thread (computer science)|thread]].
Alternatively, using the <code>ForkingMixIn</code> would cause the process to be [[fork (operating system)|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.
==Commentary==
Some of the functionality of mixins is provided by [[interface (computer science)|interfaces]] in popular languages like Java and C#. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for refactoring common behavior into a single place.
Interfaces combined with [[aspect-oriented programming]] can produce full fledged mixins in languages that support such features, such as C# or Java. Additionally, through the use of the [[marker interface pattern]], [[generic programming]], and extension methods, C# 3.0 has the ability to mimic mixins.{{Fact|date=January 2009}}
==See also==
* [[abstract type]]
* [[trait (abstract type)]]
* [[Policy-based design]]
==References==
<references />
== External links ==
* [http://c2.com/cgi/wiki?MixIn Wiki entry] at Portland Pattern Repository
* [http://www.waysys.com/ws_content_bl_fuood_ch13.html Mix-in Classes chapter] from Fundamentals of Object-Oriented Design book by Meilir Page-Jones.
* [http://www.macromedia.com/support/documentation/en/flex/1/mixin/index.html Mixins] in [[ActionScript]].
* [http://scala.epfl.ch/intro/mixin.html Scala Overview: Mixin Class Composition] - a step-by-step example in [[Scala programming language|Scala]]
* [http://www.dreamsongs.com/NewFiles/ECOOP.pdf The Common Lisp Object System: An Overview] by [[Richard P. Gabriel]] and Linda DeMichiel provides a good introduction to the motivation for defining classes by means of generic functions.
[[Category:Object-oriented programming languages]]
[[de:Mixin]]
[[es:Mixin]]
[[fr:Mixin]]
[[nl:Mixin]]
[[ja:Mixin]]
[[ru:Примесь (программирование)]]' |