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. An object-oriented Programming language (also called an OO language) is one that allows or encourages to some degree Object-oriented programming In Object-oriented programming, a class is a Programming language construct that is used as a blueprint to create objects This blueprint includes attributes In Object-oriented programming, inheritance is a way to form new classes (instances of which are called objects using classes that have already been defined 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. Multiple inheritance refers to a feature of some object-oriented Programming languages in which a class can inherit behaviors and features from
Mixins first appeared in the Symbolics' object-oriented Flavors system, which was an approach to object-orientation used in Lisp Machine Lisp. Flavors, an early object-oriented extension to Lisp developed by Howard Cannon at the MIT Artificial Intelligence Laboratory for the Lisp Machine Lisp is a dialect of the Lisp programming language, a direct descendant of Maclisp, and was initially developed in the mid to late 1970s as the systems 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. Somerville (pronunciation ˈsʌmərvɪl is a city in Middlesex County, Massachusetts, just north of Boston. ) 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. [2]
Mixins encourage code reuse and avoid well-known pathologies associated with multiple inheritance. Code reuse, also called software reuse is the use of existing software or software knowledge to build new software 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. In Computer science, runtime or run time describes the operation of a Computer program, the duration of its execution from beginning to termination In Computer science, compile time refers to either the operations performed by a Compiler (the "compile-time operations" or Programming language 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. Simula is a name for two programming languages Simula I and Simula 67 developed in the 1960s at the Norwegian Computing Center in Oslo, by Ole-Johan Dahl
Contents |
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. Simula is a name for two programming languages Simula I and Simula 67 developed in the 1960s at the Norwegian Computing Center in Oslo, by Ole-Johan Dahl
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". The Common Lisp Object System (CLOS is the facility for Object-oriented programming which is part of ANSI Common Lisp. In certain systems for Object-oriented programming such as the Common Lisp Object System and Dylan, a generic function is an entity made up of all methods 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:
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. The D programming language, also known simply as D, is an object-oriented, imperative, multiparadigm System programming language The Dylan Programming language is a multi-paradigm language that includes support for functional NOTES FOR EDITORS "Perl" is not an acronym (read the "Name" section below Python is a general-purpose High-level programming language. Its design philosophy emphasizes programmer productivity and code readability Ruby is a dynamic, reflective, general purpose Object-oriented programming language that combines syntax inspired by Perl with Smalltalk Scala ( Scalable Language) is a multi-paradigm Programming language designed to integrate features of Object-oriented programming and Strongtalk is a Smalltalk environment with optional Static typing support XOTcl is an Object-oriented extension for the Tool Command Language created by G Tcl (originally from "Tool Command Language" but nonetheless conventionally rendered as "Tcl" rather than "TCL" pronounced as " tickle " ECMAScript is a Scripting language, standardized by Ecma International in the ECMA-262 specification. Note that this is not possible with statically typed languages, where an object's signature is fixed at compile time. In Computer science, a type system defines how a Programming language classifies values and expressions into '''types''', how it can
In Python, the SocketServer module has both a UDPServer and TCPServer class that act as a server for UDP and TCP socket servers. Python is a general-purpose High-level programming language. Its design philosophy emphasizes programmer productivity and code readability User Datagram Protocol ( UDP) is one of the core protocols of the Internet Protocol Suite. The Transmission Control Protocol (TCP is one of the core protocols of the Internet Protocol Suite. Normally, all new connections are handled within the same process. Additionally, there are two mixin classes: ForkingMixIn and ThreadingMixIn. By extending TCPServer with the ThreadingMixIn like this
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass
the ThreadingMixIn class adds functionality to the TCP server such that each new connection creates a new thread. A thread in Computer science is short for a thread of execution. Alternatively, using the ForkingMixIn would cause the process to be forked for each new connection. In Computing, when a process forks, it creates a copy of itself which is called a " child process. 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.
Some of the functionality of mixins is provided by interfaces in popular languages like Java and C#. Interface generally refers to an abstraction that an entity provides of itself to the outside However, since an interface only specifies what the class must support and cannot provide an implementation, it is only useful for providing polymorphism. In Computer science, polymorphism is a Programming language feature that allows values of different Data types to be handled using a 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. Aspect-oriented programming ( AOP) is a Programming paradigm that increases modularity by allowing the separation of Cross-cutting concerns