Mixin
Mixin是面向对象程序设计语言中的类,提供了方法的实现。其他类可以访问mixin类的方法而不必成为其子类。[1]Mixin有时被称作"included"而不是"inherited"。mixin为使用它的class提供额外的功能,但自身却不单独使用(不能单独生成实例对象,属于抽象类)。因为有以上限制,Mixin类通常作为功能模块使用,在需要该功能时“混入”,而且不会使类的关系变得复杂。使用者与Mixin不是“is-a”的关系,而是「-able」关系。 Mixin有利于代码复用[2]又避免了多继承的复杂。[3][4]使用Mixin享有单一继承的单纯性和多重继承的共有性。接口与mixin相同的地方是都可以多继承,不同的地方在于mixin是带实现的。Mixin也可以看作是带实现的interface。这种设计模式实现了依赖反转原则。[5] 历史Mixin最初出现在Symbolics.com的面向对象Flavors系统(由Howard Cannon开发),使用了Lisp Machine Lisp的面向对象方法。名称起源于马萨诸塞州萨默维尔的Steve's Ice Cream:[6]这家冰淇淋店提供基本口味的冰淇淋(香草、巧克力等),混合入其他额外成分(坚果、曲奇、乳脂軟糖等)并称这些为"mix-in",还注册了商标。[7] 实现
已隱藏部分未翻譯内容,歡迎參與翻譯。 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: An example is the In an OOPSLA 90 paper,[8] 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的部分),其他语言的支持:
一些语言允许运行时从一个对象拷贝方法到另一个对象。这可以“借”mixin的方法。 C♯与Visual Basic.NET支持接口的扩展方法(extension method)。 例子Common Lisp
已隱藏部分未翻譯内容,歡迎參與翻譯。 Common Lisp 在CLOS (Common Lisp Object System)也提供Minin設計方法,其相似於Flavors。
看個例子: (defgeneric object-width (object)
(:method-combination +))
(defclass button ()
((text :initform "click me")))
接著實現button物件的方法object-with,會計算文字長度後乘10作為回傳值。結合 另外,使用:method-combination定義個通用函式,在之後定義的方法,必須都有相同簽名。意思是對於同樣名稱方法object-width,其 (defmethod object-width + ((object button))
(* 10 (length (slot-value object 'text))))
定義另一個類別 (defclass border-mixin () ())
定義其 (defmethod object-width + ((object border-mixin))
4)
最後, (defclass bordered-button (border-mixin button) ())
我們現在可以呼叫button的object-width,會得到 80。這個結果只呼叫了 ? (object-width (make-instance 'button))
80
嘗試呼叫 ? (object-width (make-instance 'bordered-button))
84
我們還可以繼續在增加 (defmethod object-width + ((object bordered-button))
2)
? (object-width (make-instance 'bordered-button))
86
PythonPython中,除了使用protocol以外,也可以用多继承的形式来实现Mixin。
Python的 class ThreadingTCPServer(ThreadingMixIn, TCPServer):
pass
Ruby在ruby中,并不直接使用Mixin这个单词,而是使用在类的声明中include一个module的办法。
已隱藏部分未翻譯内容,歡迎參與翻譯。 Most of the Ruby world is based around mixins via Example: 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可以用 对象-文字与 |