Mixin

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

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

实现

编程语言支持

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

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

C#Visual Basic.NET支持接口的扩展方法(extension method)。

例子

Common Lisp

Python

Python中,除了使用protocol以外,也可以用多继承的形式来实现Mixin。为了区分普通的多继承,mixin类的类名一般都会带上后缀:“Mixin”,比如Python 2中的类UserDict.DictMixinDictMixin类包括部分实现,使用者的类只要实现几个必须的函数接口,如:__getitem__(), __setitem__(), __delitem__(), keys()[10]

Python的SocketServer模块[11]提供了UDPServer类与TCPServer类,作为UDPTCPsocket服务器。有两个mixin类:ForkingMixInThreadingMixIn。通过如以下代码的方式使用ThreadingMixIn扩展TCPServer

class ThreadingTCPServer(ThreadingMixIn, TCPServer):
  pass

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

Ruby

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

JavaScript

其他语言

接口与trait

参见

参考文献

  1. . [2017-11-22]. (原始内容存档于2005-02-07).
  2. . [2017-11-22]. (原始内容存档于2018-01-28).
  3. . [2017-11-22]. (原始内容存档于2018-04-15).
  4. Boyland, John; Giuseppe Castagna. . Pierre Cointe (编). . Springer. 26 June 1996: 16–17 [17 January 2014]. ISBN 9783540614395. (原始内容存档于2020-06-13).
  5. . [2017-11-22]. (原始内容存档于2015-09-25).
  6. (PDF). [2017-11-22]. (原始内容存档 (PDF)于2019-02-14).
  7. slava. . concatenative.org. 2010-01-25 [2012-05-15]. (原始内容存档于2012-01-19). Factor's main language features: … Object system with Inheritance, Generic functions, Predicate dispatch and Mixins
  8. . École polytechnique fédérale de Lausanne. [16 May 2014]. (原始内容存档于2019-07-26).
  9. . [2017-11-22]. (原始内容存档于2019-01-02).
  10. . [2019-02-02]. (原始内容存档于2019-02-02).
  11. . [2017-11-22]. (原始内容存档于2015-10-24).
  12. . [2017-11-22]. (原始内容存档于2019-10-21).
  13. . [2017-11-22]. (原始内容存档于2015-09-21).
  14. . [2017-11-22]. (原始内容存档于2019-10-19).
  15. . [2017-11-22]. (原始内容存档于2017-07-09).
  16. . [2017-11-22]. (原始内容存档于2009-12-15).
  17. . [2017-11-22]. (原始内容存档于2018-03-03).
  18. The many talents of JavaScript for generalizing Role Oriented Programming approaches like Traits and Mixins 页面存档备份,存于, April 11, 2014.
  19. Angus Croll, A fresh look at JavaScript Mixins 页面存档备份,存于, published May 31, 2011.
  20. JavaScript Code Reuse Patterns 页面存档备份,存于, April 19, 2013.
  21. . [2017-11-22]. (原始内容存档于2017-07-27).

外部链接

This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.