Mixin: Difference between revisions
wp:bold and rough try at defining ''mixin'' |
|||
Line 21: | Line 21: | ||
==Commentary== |
==Commentary== |
||
Some of the functionality of mixins is provided by [[interface (computer science)|interfaces]] in popular languages like Java and C#, but since an interface only specifies what the class must support and cannot provide an implementation, they are only useful for providing [[Polymorphism (computer science)|polymorphism]]. Another class, providing an implementation and dependent with the interface, is useful for refactoring common behavior into a single place. |
Some of the functionality of mixins is provided by [[interface (computer science)|interfaces]] in popular languages like Java and C#, but since an interface only specifies what the class must support and cannot provide an implementation, they are only useful for providing [[Polymorphism (computer science)|polymorphism]]. Another class, providing an implementation and dependent with the interface, is useful for refactoring common behavior into a single place. |
||
Interfaces when combined with [[Aspect_Oriented_Programming]] can produce full fledged mixins in java and c#. |
|||
==See also== |
==See also== |
Revision as of 14:01, 22 June 2006
This article provides insufficient context for those unfamiliar with the subject. |
In object-oriented programming languages, a mixin is an approach to implementing classes which 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, to define 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, but 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, and so 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:
Commentary
Some of the functionality of mixins is provided by interfaces in popular languages like Java and C#, but since an interface only specifies what the class must support and cannot provide an implementation, they are 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 when combined with Aspect_Oriented_Programming can produce full fledged mixins in java and c#.
See also
External links
- Wiki entry at Cunningham & Cunningham, Inc.
- Mixins in ActionScript.
- Scala Overview: Mixin Class Composition - a step-by-step example in Scala
- 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.