All Topics  
Mixin

 

   Email Print
   Bookmark   Link






 

Mixin



 
 
In object-oriented programming language
Object-oriented programming language

An object-oriented programming language is one that allows or encourages, to some degree, object-oriented programming techniques such as Information hiding, Inheritance , module , and Polymorphism ....
s, a mixin is a class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 that provides a certain functionality to be inherited
Inheritance (computer science)

In object-oriented programming, inheritance is a way to form new class es using classes that have already been defined. The inheritance concept was invented in 1967 for Simula....
 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
Multiple inheritance

Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
.

Mixins first appeared in the Symbolics' object-oriented Flavors system, which was an approach to object-orientation used in Lisp Machine Lisp
Lisp Machine Lisp

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 programming language for the Massachusetts Institute of Technology Lisp machines....
.






Discussion
Ask a question about 'Mixin'
Start a new discussion about 'Mixin'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In object-oriented programming language
Object-oriented programming language

An object-oriented programming language is one that allows or encourages, to some degree, object-oriented programming techniques such as Information hiding, Inheritance , module , and Polymorphism ....
s, a mixin is a class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 that provides a certain functionality to be inherited
Inheritance (computer science)

In object-oriented programming, inheritance is a way to form new class es using classes that have already been defined. The inheritance concept was invented in 1967 for Simula....
 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
Multiple inheritance

Multiple inheritance refers to a feature of some object-oriented programming programming languages in which a class can inheritance behaviors and features from more than one superclass ....
.

Mixins first appeared in the Symbolics' object-oriented Flavors system, which was an approach to object-orientation used in Lisp Machine Lisp
Lisp Machine Lisp

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 programming language for the Massachusetts Institute of Technology Lisp machines....
. The name was inspired by Steve's Ice Cream Parlor
Steve's Ice Cream

Steve's Ice Cream was an ice-cream parlor chain restaurant in the Boston, Massachusetts region that helped create the modern concept of super-premium ice cream and created the concept of customized ice cream desserts using the mix-in....
 in Somerville, Massachusetts
Somerville, Massachusetts

Somerville is a city in Middlesex County, Massachusetts, Massachusetts, just north of Boston. As of the 2000 census, the city had a total population of 77,478 and was the most densely populated municipality in New England....
: 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
Mix-in

A mix-in is a term used to describe a dessert made of ice cream and another flavoring such as candy. Mix-in desserts are traditionally sold in an ice cream parlor and are made at the time of ordering....
", his own trademarked term at the time.

Mixins encourage code reuse
Code reuse

Code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software....
 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
Interface

Interface may refer to:...
 with implemented methods. When a class includes a mixin, the class implements the interface and includes, rather than inherit, 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
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
, though attributes and instantiation parameters are still defined at compile time
Compile time

In computer science, compile time refers to either the operations performed by a compiler , programming language requirements that must be met by source code for it to be successfully compiled , or properties of the program that can be reasoned about at compile time....
. This differs from the most widely-used approach, which originated in the programming language Simula
Simula

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 and Kristen Nygaard....
, of defining all attributes, methods and initialization at compile time.

Definition and implementation


In Simula
Simula

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 and Kristen Nygaard....
, 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
CLOS

The Common Lisp Object System is the facility for object-oriented programming which is part of ANSI Common Lisp. CLOS is a dynamic programming language object system which differs radically from the OOP facilities found in more static languages such as C++ or Java ....
, and are called "generic function
Generic function

In certain systems for object-oriented programming such as the CLOS and Dylan programming language, a generic function is an entity made up of all methods having the same name....
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++
    C++

    C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
  • ColdFusion
    ColdFusion

    ColdFusion is an application server and software language used for Internet application development such as for dynamic web page. In this regard, ColdFusion is a similar product to Microsoft Active Server Pages, JavaServer Pages or PHP....
     (Class based using includes and Object based by assigning methods from one object to another at runtime)
  • D (called )
  • Factor
    Factor programming language

    Factor is a dynamically typed concatenative programming language programming language whose design and implementation is led by Slava Pestov. Factor's main influences are Joy programming language, Forth , Lisp programming language and Self programming language....
  • Perl
    Perl

    In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
  • PLT Scheme
    PLT Scheme

    PLT Scheme is a programming language used primarily for educational purposes, produced by "PLT". It consists of four primary components:* DrScheme is the PLT Scheme program development environment....
     ()
  • Python
    Python (programming language)

    Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
  • Ruby
  • Scala
  • Smalltalk
    Smalltalk

    Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
  • Strongtalk
    Strongtalk

    Strongtalk is a Smalltalk environment with optional static typing support. Strongtalk can make some compile time checks, and offer "stronger" type-safety guarantees; this is the source of its name....
  • XOTcl
    XOTcl

    XOTcl is an object-oriented extension for the Tcl created by G. Neumann and U. Zdun. It supports metaclasses. Class and Method definitions are completely dynamic....
     (an object system for Tcl)


Some languages like ECMAScript
ECMAScript

ECMAScript is a scripting language, standardized by Ecma International in the ECMA-262 Specification . The language is widely used on the World Wide Web, and is often confused with JavaScript or JScript, the two major Programming language dialect from which ECMAScript was standardized....
 (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 statically typed languages, where an object's signature is fixed at compile time.

Example

In Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, the SocketServer module has both a UDPServer and TCPServer class that act as a server for UDP
User Datagram Protocol

The User Datagram Protocol is one of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer applications can send messages, sometimes known as datagram, to other hosts on an Internet Protocol network without requiring prior communications to set up special transmission cha...
 and TCP
Transmission Control Protocol

The Transmission Control Protocol is one of the core protocols of the Internet Protocol Suite. TCP is so central that the entire suite is often referred to as "TCP/IP"....
 socket servers. 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
Thread (computer science)

In computer science, a thread of execution is a Fork of a computer program into two or more Concurrency running task s. The implementation of threads and process es differs from one operating system to another, but in most cases, a thread is contained inside a process....
. Alternatively, using the ForkingMixIn would cause the process to be forked
Fork (operating system)

In computing, when a Computer_process forks, it creates a copy of itself, which is called a "Child_process." The original process is then called the "Parent_process"....
 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 interfaces
Interface (computer science)

Interface generally refers to an Abstraction_%28computer_science%29 that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide Polymorphism in object-orien...
 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
Aspect-oriented programming

Aspect-oriented programming is a programming paradigm that increases Modularity by allowing the separation of concerns cross-cutting concerns, forming a basis for Aspect-oriented software development....
 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
Marker interface pattern

The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects....
, generic programming
Generic programming

Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters and was pioneered by Ada which appeared in 1983....
, and extension methods, C# 3.0 has the ability to transparently mimic mixins.

See also

  • abstract type
    Abstract type

    In software engineering, an abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains members which are also members of some declared subtype....
  • trait (abstract type)
    Trait (abstract type)

    In computer programming, a trait is an abstract type, used as a "simple conceptual model for structuring object oriented programs". Traits are similar to mixins, but may include definitions for class methods....
  • Policy-based design
    Policy-based design

    Policy-based design, also known as policy-based class design or policy-based programming, is a computer programming paradigm based on an idiom#Computer science for C++ known as policies....


External links

  • at Portland Pattern Repository
  • from Fundamentals of Object-Oriented Design book by Meilir Page-Jones.
  • in ActionScript
    ActionScript

    ActionScript is a scripting language based on ECMAScript. ActionScript is used primarily for the development of websites and software using the Adobe Flash Player platform , but is also used in some database applications , and in basic robotics, as with the Make Controller Kit....
    .
  • - a step-by-step example in Scala
    Scala programming language

    Scala is a multi-paradigm programming language programming language designed to integrate features of object-oriented programming and functional programming....
  • by Richard P. Gabriel and Linda DeMichiel provides a good introduction to the motivation for defining classes by means of generic functions.