Mixin

Mixin是面向对象程序设计语言中的类，提供了方法的实现。其他类可以访问mixin类的方法而不必成为其子类。Mixin有时被称作"included"而不是"inherited"。mixin为使用它的class提供额外的功能，但自身却不单独使用（不能单独生成实例对象，属于抽象类）。因为有以上限制，Mixin类通常作为功能模块使用，在需要该功能时“混入”，而且不会使类的关系变得复杂。使用者与Mixin不是“is-a”的关系，而是“-able”关系。

Mixin有利于代码复用又避免了多继承的复杂。使用Mixin享有单一继承的单纯性和多重继承的共有性。接口与mixin相同的地方是都可以多继承，不同的地方在于mixin是带实现的。Mixin也可以看作是带实现的interface。这种设计模式实现了依赖反转原则。

## 历史

Mixin最初出现在Symbolics.com的面向对象Flavors系统（由Howard Cannon开发），使用了Lisp Machine Lisp的面向对象方法。名称起源于马萨诸塞州萨默维尔的Steve's Ice Cream：这家冰淇淋店提供基本口味的冰淇淋（香草、巧克力等），混合入其他额外成分（坚果、曲奇、乳脂软糖等）并称这些为"mix-in",还注册了商标。

## 实现

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.

In Flavors, a Mixin is a class from which another class can inherit slot definitions and methods. The Mixin usually does not have direct instances. Since a Flavor can inherit from more than one other Flavor, it can inherit from one or more Mixins. Note that the original Flavors did not use generic functions.

In New Flavors (a successor of Flavors) and CLOS, methods are organized in "generic functions". These generic functions are functions that are defined in multiple cases (methods) by class dispatch and method combinations.

CLOS and Flavors allow mixin methods to add behavior to existing methods: `:before` and `:after` daemons, whoppers and wrappers in Flavors. CLOS added `:around` methods and the ability to call shadowed methods via CALL-NEXT-METHOD. So, for example, a stream-lock-mixin can add locking around existing methods of a stream class. In Flavors one would write a wrapper or a whopper and in CLOS one would use an `:around` method. Both CLOS and Flavors allow the computed reuse via method combinations. `:before`, `:after` and `:around` methods are a feature of the standard method combination. Other method combinations are provided.

An example is the `+` method combination, where the resulting values of each of the applicable methods of a generic function are arithmetically added to compute the return value. This is used, for example, with the border-mixin for graphical objects. A graphical object may have a generic width function. The border-mixin would add a border around an object and has a method computing its width. A new class `bordered-button` (that is both a graphical object and uses the `border` mixin) would compute its width by calling all applicable width methods—via the `+` method combination. All return values are added and create the combined width of the object.

In an OOPSLA 90 paper, Gilad Bracha and William Cook reinterpret different inheritance mechanisms found in Smalltalk, Beta and CLOS as special forms of a mixin inheritance.

## 编程语言支持

除了Flavors与CLOS (作为Common Lisp的部分)，其他语言的支持:

- Ada语言 (扩展已存在的tagged record)
- Cobra
- ColdFusion (基于类的includes，基于对象的赋值方法)
- Curl (Curl RTE)
- D语言 (称作 "template mixins" ; D语言包含 "mixin" 语句.)
- Dart
- Factor语言
- Groovy
- JavaScript Delegation - Functions 作为 Roles (Traits 与 Mixins)
- OCaml
- Perl (通过 Moose_(Perl)的roles)
- Perl 6
- PHP的"Trait"
- Python
- Racket ( mixins documentation )
- Ruby
- Scala
- XOTcl/ TclOO
- Sass
- Vala
- Swift语言
- SystemVerilog

一些语言允许运行时从一个对象拷贝方法到另一个对象。这可以“借”mixin的方法。

C♯与Visual Basic.NET支持接口的扩展方法（extension method）。

## 例子

### Common Lisp

Common Lisp 在CLOS (Common Lisp Object System)也提供Minin设计方法，其相似于Flavors。

`object-width` 是一个接受一个参数的通用函式，并使用`+`方式结合。这种结合方式，会让所有应用这个方法的互叫，自动以加法结合其结果。

看个例子：

```lisp
(defgeneric object-width (object)
  (:method-combination +))
```

`button` 类别拥有一段文字。

```lisp
(defclass button ()
  ((text :initform "click me")))
```

接着实现button物件的方法object-with，会计算文字长度后乘10作为回传值。结合`+`方法，会使此方法每次都被呼叫，并以加法结合(继续看下面例子)。

另外，使用:method-combination定义个通用函式，在之后定义的方法，必须都有相同签名。意思是对于同样名称方法object-width，其`+`并不可省(将会报错)。

```lisp
(defmethod object-width + ((object button))
   (* 10 (length (slot-value object 'text))))
```

定义另一个类别`border-mixin`。做为示例，只有名字没有特别成员、没有父类别。

```lisp
(defclass border-mixin () ())
```

定义其`object-width +`方法。单纯的回传4。

```lisp
(defmethod object-width + ((object border-mixin))
  4)
```

最后，`bordered-button`类别同时继承了`border-mixin`和`button`.

```lisp
(defclass bordered-button (border-mixin button) ())
```

我们现在可以呼叫button的object-width，会得到 80。这个结果只呼叫了`button`这意类别的`object-width`方法。

(预设字串"click me"长度为8，8*10=80)

```lisp
? (object-width (make-instance 'button))
80
```

尝试呼叫`bordered-button`的`object-width`，会得到84。这结果是呼叫`button`的`object-width`和`border-mixin`的`object-width`结果总和。

```lisp
? (object-width (make-instance 'bordered-button))
84
```

我们还可以继续在增加`bordered-button`的`object-width`方法。很简单的想让结果在加2。

```lisp
(defmethod object-width + ((object bordered-button))
  2)

? (object-width (make-instance 'bordered-button))
86
```

### Python

Python中，除了使用protocol以外，也可以用多继承的形式来实现Mixin。

- 首先它必须表示某一种功能，而不是某个物品。Mixin必须责任单一，如果有多个功能，那就写多个Mixin类。
- Mixin不依赖于子类的实现
- 子类即便没有继承这个Mixin类，也照样可以工作，只是缺少了某个功能
- 多重继承时候，Mixin类应该在基本的父类之前（即左侧）
- 为了区分普通的多继承，mixin类的类名一般都会带上后缀：Mixin、able、ible等。比如Python 2中的类`UserDict.DictMixin`。`DictMixin`类包括部分实现，使用者的类只要实现几个必须的函数接口，如：`__getitem__()`, `__setitem__()`, `__delitem__()`, `keys()`。

Python的`SocketServer`模块提供了`UDPServer`类与`TCPServer`类，作为UDP与TCP的socket服务器。有两个mixin类：`ForkingMixIn`与 `ThreadingMixIn`。通过如以下代码的方式使用`ThreadingMixIn`扩展`TCPServer`：

```python
class ThreadingTCPServer(ThreadingMixIn, TCPServer):
  pass
```

`ThreadingMixIn`类为TCP服务器添加了新功能，使每个新连接都会创建出新线程。而如果是`ForkingMixIn`，则会使每个新连接fork出新的进程。

### Ruby

在ruby中，并不直接使用Mixin这个单词，而是使用在类的声明中include一个module的办法。

Most of the Ruby world is based around mixins via `Modules`. The concept of mixins is implemented in Ruby by the keyword `include` to which we pass the name of the module as parameter.

Example:

```ruby
class Student
  include Comparable # The class Student inherits the Comparable module using the 'include' keyword
  attr_accessor :name, :score

  def initialize(name, score)
    @name = name
    @score = score
  end

  # Including the Comparable module requires the implementing class to define the <=> comparison operator
  # Here's the comparison operator. We compare 2 student instances based on their scores.

  def <=>(other)
    @score <=> other.score
  end

  # Here's the good bit - I get access to <, <=, >,>= and other methods of the Comparable Interface for free.
end

s1 = Student.new("Peter", 100)
s2 = Student.new("Jason", 90)

s1 > s2 #true
s1 <= s2 #false
```

### JavaScript

在JavaScript中，Mixin可以用`Object.assign(MyClass.prototype, MixinClass);`实现。Mixin可以有自己的父类。

#### 对象-文字与`extend`方法

技术上可以通过绑定函数到对象的键，来给对象增加行为。但这导致在状态与行为之间缺少分离等缺点：

- 它扰乱了模型域属性与实现域属性。
- 共同的行为没有共享。元对象解决此问题通过分离对象的域相关属性与行为相关的属性。

一个扩展函数(来自Underscore.js库，把源对象的所有功能复制到目标对象, 特性, 函数等)用于混合行为：

```javascript
// This example may be contrived.
// It's an attempt to clean up the previous, broken example.
var Halfling = function (fName, lName) {
    this.firstName = fName;
    this.lastName = lName;
}

var NameMixin = {
    fullName: function () {
        return this.firstName + ' ' + this.lastName;
    },
    rename: function(first, last) {
        this.firstName = first;
        this.lastName = last;
        return this;
    }
};

var sam = new Halfling('Sam', 'Lowry');
var frodo = new Halfling('Freeda', 'Baggs');

// Mixin the other methods
_.extend(Halfling.prototype, NameMixin);

// Now the Halfling objects have access to the NameMixin methods
sam.rename('Samwise', 'Gamgee');
frodo.rename('Frodo', 'Baggins');
```

#### 基于飞行Mixin方法的纯函数与委托

上述描述方法得到广泛使用。但下述方法更接近于JavaScript语言的基础核心 - Delegation.

两个基于模式的函数对象不需要`extend`的第三方实现就可完成这种技巧。

```javascript
// Implementation
var EnumerableFirstLast = (function () { // function based module pattern.
    var first = function () {
        return this[0];
    },
    last = function () {
        return this[this.length - 1];
    };
    return function () {      // function based Flight-Mixin mechanics ...
        this.first  = first;  // ... referring to ...
        this.last   = last;   // ... shared code.
    };
}());

// Application - explicit delegation:
// applying [first] and [last] enumerable behavior onto [Array]'s [prototype].
EnumerableFirstLast.call(Array.prototype);

// Now you can do:
a = [1, 2, 3];
a.first(); // 1
a.last();  // 3
```

### 其他语言

In the Curl web-content language, multiple inheritance is used as classes with no instances may implement methods. Common mixins include all skinnable `ControlUI`s inheriting from `SkinnableControlUI`, user interface delegate objects that require dropdown menus inheriting from StandardBaseDropdownUI and such explicitly named mixin classes as `FontGraphicMixin`, `FontVisualMixin` and `NumericAxisMixin-of` class. Version 7.0 added library access so that mixins do not need to be in the same package or be public abstract. Curl constructors are factories that facilitates using multiple-inheritance without explicit declaration of either interfaces or mixins.

## 接口与trait

Java 8 introduces a new feature in the form of default methods for interfaces. Basically it allows a method to be defined in an interface with application in the scenario when a new method is to be added to an interface after the interface class programming setup is done. To add a new function to the interface means to implement the method at every class which uses the interface. Default methods help in this case where they can be introduced to an interface any time and have an implemented structure which is then used by the associated classes. Hence default methods adds a possibility of applying the concept in a mixin sort of a way.

Interfaces combined with aspect-oriented programming can also produce full-fledged mixins in languages that support such features, such as C# or Java. Additionally, through the use of the marker interface pattern, generic programming, and extension methods, C# 3.0 has the ability to mimic mixins. With C# 3.0 came the introduction of Extension Methods2 and they can be applied, not only to classes but, also, to interfaces. Extension Methods provide additional functionality on an existing class without modifying the class. It then becomes possible to create a static helper class for specific functionality that defines the extension methods. Because the classes implement the interface (even if the actual interface doesn’t contain any methods or properties to implement) it will pick up all the extension methods also.

ECMAScript (in most cases implemented as JavaScript) does not need to mimic object composition by stepwise copying fields from one object to another. It natively supports Trait and Mixin based object composition via function objects that implement additional behavior and then are delegated via `call` or `apply` to objects that are in need of such new functionality.

### Scala

Scala has a rich type system and Traits are a part of Scala’s type system which help implement mixin behaviour. As their name reveals, Traits are usually used to represent a distinct feature or aspect that is normally orthogonal to the responsibility of a concrete type or at least of a certain instance.

For example, the ability to sing is modeled as such an orthogonal feature: it could be applied to Birds, Persons, etc.

```scala
trait Singer{
  def sing { println(" singing … ") }
  //more methods
}

class Birds extends Singer
```

Here, Bird has mixed in all methods of the trait into its own definition as if class Bird had defined method sing on its own.

As `extends` is also used to inherit from a super class, in case of a trait `extends` is used if no super class is inherited and only for mixin in the first trait. All following traits are mixed in using keyword `with`.

```scala
class Person
class Actor extends Person with Singer
class Actor extends Singer with Performer
```

Scala allows mixing in a trait (creating an anonymous type) when creating a new instance of a class. In the case of a Person class instance, not all instances can sing. This feature comes use then:

```scala
class Person{
  def tell {  println (" Human ") }
  //more methods
}

val singingPerson = new Person with Singer
singingPerson.sing
```

### Swift

Mixin can be achieved in Swift by using a language feature called Default implementation in Protocol Extension.

```swift
protocol ErrorDisplayable {
    func error(message:String)
}

extension ErrorDisplayable {
    func error(message:String) {
        // Do what it needs to show an error
        //...
        print(message)
    }
}

struct NetworkManager : ErrorDisplayable{
    func onError() {
        error("Please check your internet Connection.")
    }
}
```

## 外部链接

- MixIn at Portland Pattern Repository
- Mixins in ActionScript
- 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.