A brief description of Mixins

Mixin (also known as a abstract subclasses) is a subclass definition which can be applied to different superclasses, to create a related family of modified classes [1]. The term mixin was coined nearly 20 years ago by the Lisp object-oriented community, the name was inspired by an ice cream store in Sommerville, Massachusetts, where candies and cakes where mixed into the basic ice cream flavors [2, 3]. The ice cream analogy can be transfered directly into classes, where the ice cream class (superclass) can get some cakes "mixed" in, making a new class. The mixin her is a subclass that represents cakes. If we wanted a new ice cream with an extra features, such as chocolate cover we could create a mixin subclass for this, and then get at ice cream with cakes and chocolate cover mixed in. The following code will give the reader an example of mixin usage, in the language MixedJava, a Java version supporting mixins, the example is is taken from [4] and describes a scenario where you want to model a door that in the case it is locked will need a key and in the case that it is short you will have to be short to to get in. The two subclasses are defined as mixins. These are then mixed into three different classes, one locked, one short and one locked and short: interface Doori { boolean canOpen(Personc p); boolean canPass(Personc p); } mixin Lockedm extends Doori { boolean canOpen(Personc p) { if(!p.hasItem(theKey)) { System.out.println("You don't have the Key"); return false; } System.out.println("Using key..."); return super.canOpen(p); } } mixin Shortm extends Doori { boolean canPass(Personc p) { if(p.height() > 1) { System.out.println("You are too tall"); return false; } System.out.println("Ducking into door..."); return super.canPass(p); } } class LockedDoorc = Lockedm(Doorc ); class ShortDoorc = Shortm(Doorc ); class LockedShortDoorc = Lockedm(Shortm(Doorc )); Mixins create a strong abstraction mechanism with many important applications, one of which is a simple, alternative to multiple inheritance, which some languages don't support and others don't encourage fore reasons of complexity and problems with debugging [3]. References [1] G. Bracha and W. Cook, Mixin-based Inheritance, ECOOP/OOPSLA '90 Proceedings, 1990. [2] Jesse Liberty, Teach Yourself C++ in 21 Days, 4. ed., Sams Publishing, 2001. [3] E. Allen, J. Bannet and R. Cartwright, A FirstClass Approach to Genericity, OOPSLA '03 Proceedings, 2003. [4] M. Flatt, S. Krishnamurthi, and M. Felleisen, A Programmer's Reduction Semantics for Classes and Mixins, LNCS 1523, Springer-Verlag Berlin Heidelberg 1999. Other references of interest * Y. Smaragdakis and D. Batory, Implementing Layered Designs with Mixin Layers, [link] * Y. Smaragdakis and D. Batory, Mixin-Based Programming in C++1, LNCS 2177, Springer-Verlag Berlin Heidelberg 2001. * D. Ancona, G. Lagorio, and E. Zucca, Jam - A Smooth Extension of Java with Mixins?, LNCS 1850, Springer-Verlag Berlin Heidelberg 2000. Course: Generative software development Author: Poul J. Clementsen Date: 2006-01-31