Table of Contents
回复内容:
Home Backend Development Python Tutorial Mixin是什么概念?

Mixin是什么概念?

Jun 06, 2016 pm 04:23 PM
auth mixin oauth openid tornado

在浏览tornado的代码时,auth中的类都以Mixin命名,这个词好奇怪啊,查了一下资料,有人解释Mixin为mix in,混入的意思,类似于多重继承。auth模块实现OpenID和OAuth,为什么要用Mixin方式?Mixin的应用场景?与“接口”概念有什么区别?

回复内容:

Mixin 实质上是利用语言特性(比如 Ruby 的 include 语法、Python 的多重继承)来更简洁地实现组合模式

以如下 Java 伪码为例,实现一个可复用的“打标签”组件(Taggable),并且应用到帖子(Post)模型上:
<span class="kn">import</span> <span class="nn">java.util.List</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="o">;</span>

<span class="kd">interface</span> <span class="nc">Entity</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getId</span><span class="o">();</span>
    <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getKind</span><span class="o">();</span>
<span class="o">}</span>

<span class="kd">interface</span> <span class="nc">Taggable</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">addTag</span><span class="o">(</span><span class="kt">int</span> <span class="n">tagId</span><span class="o">);</span>
    <span class="kd">public</span> <span class="n">List</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span> <span class="nf">getTags</span><span class="o">();</span>
<span class="o">}</span>

<span class="kd">class</span> <span class="nc">TaggableImpl</span> <span class="kd">implements</span> <span class="n">Taggable</span> <span class="o">{</span>
    <span class="kd">private</span> <span class="n">Entity</span> <span class="n">target</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">TaggableImpl</span><span class="o">(</span><span class="n">Entity</span> <span class="n">target</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">target</span> <span class="o">=</span> <span class="n">target</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">addTag</span><span class="o">(</span><span class="kt">int</span> <span class="n">tagId</span><span class="o">)</span> <span class="o">{</span>
        <span class="kt">int</span> <span class="n">id</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="na">getId</span><span class="o">();</span>
        <span class="kt">int</span> <span class="n">kind</span> <span class="o">=</span> <span class="n">target</span><span class="o">.</span><span class="na">getKind</span><span class="o">();</span>
        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"insert into ... values "</span>
                <span class="o">+</span> <span class="n">id</span> <span class="o">+</span> <span class="s">", "</span>
                <span class="o">+</span> <span class="n">kind</span> <span class="o">+</span> <span class="s">", "</span>
                <span class="o">+</span> <span class="n">tagId</span> <span class="o">+</span> <span class="s">")"</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span> <span class="nf">getTags</span><span class="o">()</span> <span class="o">{</span>
        <span class="c1">// query from database</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Integer</span><span class="o">>();</span>
    <span class="o">}</span>
<span class="o">}</span>

<span class="kd">class</span> <span class="nc">Post</span> <span class="kd">implements</span> <span class="n">Entity</span><span class="o">,</span> <span class="n">Taggable</span> <span class="o">{</span>
    <span class="kd">public</span> <span class="kd">final</span> <span class="kd">static</span> <span class="kt">int</span> <span class="n">KIND</span> <span class="o">=</span> <span class="mi">1001</span><span class="o">;</span>

    <span class="kd">private</span> <span class="n">Taggable</span> <span class="n">taggable</span><span class="o">;</span>
    <span class="kd">private</span> <span class="kt">int</span> <span class="n">id</span><span class="o">;</span>
    <span class="kd">private</span> <span class="n">String</span> <span class="n">title</span><span class="o">;</span>

    <span class="kd">public</span> <span class="nf">Post</span><span class="o">(</span><span class="kt">int</span> <span class="n">id</span><span class="o">,</span> <span class="n">String</span> <span class="n">title</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">title</span> <span class="o">=</span> <span class="n">title</span><span class="o">;</span>
        <span class="k">this</span><span class="o">.</span><span class="na">taggable</span> <span class="o">=</span> <span class="k">new</span> <span class="n">TaggableImpl</span><span class="o">(</span><span class="k">this</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">id</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getKind</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">KIND</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">addTag</span><span class="o">(</span><span class="kt">int</span> <span class="n">tagId</span><span class="o">)</span> <span class="o">{</span>
        <span class="n">taggable</span><span class="o">.</span><span class="na">addTag</span><span class="o">(</span><span class="n">tagId</span><span class="o">);</span>  <span class="c1">// delegate</span>
    <span class="o">}</span>

    <span class="kd">public</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Integer</span><span class="o">></span> <span class="nf">getTags</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">taggable</span><span class="o">.</span><span class="na">getTags</span><span class="o">();</span>  <span class="c1">// delegate</span>
    <span class="o">}</span>
<span class="o">}</span>
Copy after login
Mixin 就是混入的意思。

和多重继承类似(其实可以把 Mixin 看作多重继承的一种在特定场景下的应用),但通常混入 Mixin 的类和 Mixin 类本身不是 is-a 的关系,混入 Mixin 类是为了添加某些(可选的)功能。自由地混入 Mixin 类就可以灵活地为被混入的类添加不同的功能。

传统的「接口」概念中并不包含实现,而 Mixin 包含实现。实际上 Mixin 的作用和 Java 中的众多以「able」结尾的接口很相似。不同的是 Mixin 提供了(默认)实现,而 Java 中实现了 -able 接口的类需要类自身来实现这些混入的功能(Serializable 接口是个例外)。 趁着午休来答一个。

如楼上很多答主一样,谈到Mixin就不得不谈到多重继承,因为Mixin的出现就是为了解决多重继承的问题,那么多重继承有什么问题呢?

在《松本行弘的程序世界》一书中,作者列举了以下三点:

  1. 结构复杂化:如果是单一继承,一个类的父类是什么,父类的父类是什么,都很明确,因为只有单一的继承关系,然而如果是多重继承的话,一个类有多个父类,这些父类又有自己的父类,那么类之间的关系就很复杂了。
  2. 优先顺序模糊:假如我有A,C类同时继承了基类,B类继承了A类,然后D类又同时继承了B和C类,所以D类继承父类的方法的顺序应该是D、B、A、C还是D、B、C、A,或者是其他的顺序,很不明确。
  3. 功能冲突:因为多重继承有多个父类,所以当不同的父类中有相同的方法是就会产生冲突。如果B类和C类同时又有相同的方法时,D继承的是哪个方法就不明确了,因为存在两种可能性。
当然你可以说有些语言解决了这个问题,但是并不是所有语言都想要去纠结这个问题。

所以为能够利用多继承的优点又解决多继承的问题,提出了规格继承和实现继承这两样东西。

简单来讲,规格继承指的是一堆方法名的集合,而实现继承除了方法名还允许有方法的实现。

Java 选择了规格继承,在 Java 中叫 interface(不过Java8中已经有默认方法了),而 Ruby 选择了实现继承,也可以叫Mixin,在 Ruby 中叫 module。

从某种程度上来说,继承强调 I am,Mixin 强调 I can。当你 implement 了这个接口或者 include 这个 module 的时候,然后就你行你上。

所以这又可以扯到 duck typing 去了,不细说。要想了解具体的可以看一下《松本行弘的程序世界》这本书。 这叫迷信方法,你想知道好处,打开py源码,搜搜 mixin,试着不用迷信实现一个本来用了迷信的模块,就能切身感受一下了。 为了解决多重继承的问题,Java引入了接口 (interface)技术Lisp、Ruby引入了 Mix-in 技术。


以 Ruby 为例,Mix-in 有效地降低多重继承复杂性(谁是你爹,哪个爹的优先级高,你的把妹方法是继承自哪个爹的等)。 Ruby中 Mix-in 的单位是 模块 (module)

Mix-in 技术按一下规则来限制多重继承:
  1. 继承用但一继承;
  2. 第二个及两个以上的父类必须是 Mix-in 的抽象类。

Mix-in 类是具有以下特征的抽象类:
  1. 不能单独生成实例;
  2. 不能继承普通类。

按照以上的原则,类在层次上具有单一继承一样的树结构,同时又可以实现功能的共享(方法是:把共享的功能放在 Mix-in 类中,再把 Mix-in 类插入到树结构里)。

Java 用 接口 解决规格继承(类都有哪些方法)的问题,Mix-in 则解决了实现继承(类中都用了什么数据结构和什么算法)的问题。

逼逼了这么多,对于 Mix-in 的理解是,Mix-in 只不过是实现多重继承的一个技巧而已。 被约束的多重继承。 mixin不是多继承,mixin是duck type的糖,让你可以不用去把一坨坨Interface继承一遍然后才能彼此调用接口。 mixin 并没有特别权威的标准,非要让我下个定论的话:mixin 其实就是在语言不提供标准多重继承的情况下,变相实现多重继承的一个语法糖。 不同版本的 mixin 实现不太一样,但出发点都是在允许继承接口和继承实现的基础上,简化继承关系,避免多重继承的坑。


我认为 C# 的扩展方法其实就是一种 mixin,但又不是通过代码拷贝等动态特性实现的,而是在编译器层面很严格的帮你进行了封装。对其它静态语言来说,如果没有多重继承,要自己实现 mixin 是很困难的。动态语言有些就支持 mixin,不支持的话自己造一个也很容易。


我最近也利用 javascript 实现了一个版本的 mixin 机制,为了支持可视化编辑,添加了类似组件系统的能力,将整合进 0.5 的 Fireball-x,到时会进一步更新答案。


手机码字求赞,欢迎更多讨论。 就是编译的时候把一段代码复制到另一个地方的意思。 Mixin是一种特殊的多重继承,也就是多重继承的子集。
使用Mixin的好处是,同时享有单一继承的单纯性和多重继承的共有性。

作为Mixin类,需要满足以下条件:
  1. 不能单独生成实例对象,属于抽象类。
  2. 不能继承Mixin以外的类。
因为有以上限制,Mixin类通常作为功能模块使用,在需要该功能时“混入”,而且不会使类的关系变得复杂(比如,同名方法到底从哪个父类继承)。
Java的接口,只提供了“规格”的多重继承。Mixin类则同时提供了“规格”和“实现”的多重继承,使用上相比接口会更加简单。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

OAuth in PHP: Create a JWT authorization server OAuth in PHP: Create a JWT authorization server Jul 28, 2023 pm 05:27 PM

OAuth in PHP: Creating a JWT authorization server With the rise of mobile applications and the trend of separation of front-end and back-end, OAuth has become an indispensable part of modern web applications. OAuth is an authorization protocol that protects users' resources from unauthorized access by providing standardized processes and mechanisms. In this article, we will learn how to create a JWT (JSONWebTokens) based OAuth authorization server using PHP. JWT is a type of

PHP and OAuth: Implementing Microsoft Login Integration PHP and OAuth: Implementing Microsoft Login Integration Jul 28, 2023 pm 05:15 PM

PHP and OAuth: Implementing Microsoft login integration With the development of the Internet, more and more websites and applications need to support users to log in using third-party accounts to provide a convenient registration and login experience. Microsoft account is one of the widely used accounts around the world, and many users want to use Microsoft account to log in to websites and applications. In order to achieve Microsoft login integration, we can use the OAuth (Open Authorization) protocol to achieve it. OAuth is an open-standard authorization protocol that allows users to authorize third-party applications to act on their behalf

PHP development: Implementing OAuth2 service provider using Laravel Passport PHP development: Implementing OAuth2 service provider using Laravel Passport Jun 15, 2023 pm 04:32 PM

With the popularity of mobile Internet, more and more applications require users to authenticate and authorize themselves. OAuth2 is a popular authentication and authorization framework that provides applications with a standardized mechanism to implement these functions. LaravelPassport is an easy-to-use, secure, and out-of-the-box OAuth2 server implementation that provides PHP developers with powerful tools for building OAuth2 authentication and authorization. This article will introduce the use of LaravelPassport

How to do Google Drive integration using PHP and OAuth How to do Google Drive integration using PHP and OAuth Jul 31, 2023 pm 04:41 PM

How to do GoogleDrive integration using PHP and OAuth GoogleDrive is a popular cloud storage service that allows users to store files in the cloud and share them with other users. Through GoogleDriveAPI, we can use PHP to write code to integrate with GoogleDrive to implement file uploading, downloading, deletion and other operations. To use GoogleDriveAPI we need to authenticate via OAuth and

OAuth2 authentication method and implementation in PHP OAuth2 authentication method and implementation in PHP Aug 07, 2023 pm 10:53 PM

OAuth2 authentication method and implementation in PHP With the development of the Internet, more and more applications need to interact with third-party platforms. In order to protect user privacy and security, many third-party platforms use the OAuth2 protocol to implement user authentication. In this article, we will introduce the OAuth2 authentication method and implementation in PHP, and attach corresponding code examples. OAuth2 is an authorization framework that allows users to authorize third-party applications to access their resources on another service provider without mentioning

Tips for using mixins to implement CRUD (add, delete, modify, check) operations in Vue Tips for using mixins to implement CRUD (add, delete, modify, check) operations in Vue Jun 25, 2023 pm 07:42 PM

Mixin in Vue is a very useful feature. It can encapsulate some reusable code in a mixin object, and then use mixin to introduce it in the components that need to use these codes. This method greatly improves the reusability and maintainability of the code, especially in some repeated CRUD (add, delete, modify) operations. This article will introduce how to use mixins to implement CRUD operations in Vue. First, we need to understand how to create a

Solution to NotImplementedError() Solution to NotImplementedError() Mar 01, 2024 pm 03:10 PM

The reason for the error is in python. The reason why NotImplementedError() is thrown in Tornado may be because an abstract method or interface is not implemented. These methods or interfaces are declared in the parent class but not implemented in the child class. Subclasses need to implement these methods or interfaces to work properly. How to solve this problem is to implement the abstract method or interface declared by the parent class in the child class. If you are using a class to inherit from another class and you see this error, you should implement all the abstract methods declared in the parent class in the child class. If you are using an interface and you see this error, you should implement all methods declared in the interface in the class that implements the interface. If you are not sure which

How to use PHP and OAuth for QQ login integration How to use PHP and OAuth for QQ login integration Jul 31, 2023 pm 12:37 PM

Introduction to how to use PHP and OAuth for QQ login integration: With the development of social media, more and more websites and applications are beginning to provide third-party login functions to facilitate users to quickly register and log in. As one of China's largest social media platforms, QQ has also become a third-party login service provided by many websites and applications. This article will introduce the steps on how to use PHP and OAuth for QQ login integration, with code examples. Step 1: Register as a QQ open platform developer. Before starting to integrate QQ login, I

See all articles