[go: nahoru, domu]

Jump to content

Mixin: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Definition and implementation: Remove PHP since it only has single-inheritence so you can't mix other classes in. The best is using an abstract class but that's not a mixin then, really.
Added an example
Line 22: Line 22:
*[[Perl]]
*[[Perl]]
*[[ColdFusion]]
*[[ColdFusion]]

==Example==
In [[Python (programming language)|Python]], the SocketServer module has both a UDPServer and TCPServer class that act as a server for [[User Datagram Protocol|UDP]] and [[Transmission Control Protocol|TCP]] socket servers.
Additionally, there are two mixin classes: ForkingMixIn and ThreadingMixIn.
By extending TCPServer with the ThreadingMixIn like this

class ThreadingTCPServer(ThreadingMixIn, TCPServer):

The ThreadingMixIn class adds functionality to the TCP server such that each new connection creates a new [[thread (computer science)|thread]].
Alternatively, using the ForkingMixIn would cause the process to be [[fork (operating system)|fork]] for each new connection.

In this usage example, the mixins provide alternative underlying functionality without affecting the functionality as a socket server.


==Commentary==
==Commentary==
Line 32: Line 44:


==See also==
==See also==

* [[abstract type]]
* [[abstract type]]
* [[trait (abstract type)]]
* [[trait (abstract type)]]

Revision as of 03:34, 26 June 2007

In object-oriented programming languages, a mixin is a class that provides a certain functionality to be 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 subclass may even choose to inherit most or all of its functionality by inheriting from one or more mixins through multiple inheritance.

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.

Mixins were first used in Flavors, which was an approach to object-orientation used in Lisp Machine Lisp. The advantage of mixins is that they encourage code reuse and avoid well-known pathologies associated with multiple inheritance. However, mixins introduce their own set of compromises.

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 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:

Example

In Python, the SocketServer module has both a UDPServer and TCPServer class that act as a server for UDP and TCP socket servers. Additionally, there are two mixin classes: ForkingMixIn and ThreadingMixIn. By extending TCPServer with the ThreadingMixIn like this

class ThreadingTCPServer(ThreadingMixIn, TCPServer):

The ThreadingMixIn class adds functionality to the TCP server such that each new connection creates a new thread. Alternatively, using the ForkingMixIn would cause the process to be fork for each new connection.

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 interfaces in popular languages like Java and C#. However, since an interface only specifies what the class must support and cannot provide an implementation, it is only useful for providing polymorphism. Another class, providing an implementation and dependent with the interface, is useful 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.

Mixins first appeared in the Symbolics' object-oriented Flavors system, and the name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts. [1] The ice cream shop owner offered a basic flavor of ice cream (vanilla, chocolate, etc.) and blended in a combination extra items (nuts, cookie, fudge, etc.) and called the item a "Mix-In", his own trademarked term at the time. [2]

See also