mixin

本文介绍了 Ruby 中的 Mix-in 概念,展示了如何通过 Mix-in 实现类的多重继承,并解释了 module 和 class 方法的区别。

Matz 的一篇 PPT “Object-Oriented scripting in Ruby”中, Matz 提到 Ruby 提供一种语言机制 Mix-in ,在其 PPT 中如是描述的“ No Multiple Inheritance but Mix-in ”、“ Mix-in is as strong as multiple inheritance but simple ”。

 

“module” in Ruby

C++ Java C# 中都有 namespace 的概念,是用来隔离代码,防止代码冲突的。在 Ruby 中是采用“ module ”这个关键字来完成 namespace 功能的。

 

//considering the following code:

//MicrosoftCompiler.rb

module Microsoft

         def Microsoft.compile

                   print "This is microsoft's compiler", "/n"

         end

end

 

//SunCompiler.rb

module Sun

         def Sun.compile

                   print "This is Sun's compiler", "/n"

         end

end

                                                                          

//TestCompiler.rb

require "D://Ruby//MicrosoftCompiler.rb"

require "D://Ruby//SunCompiler.rb"

 

Microsoft.compile

Sun.compile

 

//output:

>ruby TestComplier.rb

This is microsoft's compiler

This is Sun's compiler

>Exit code: 0

 

module 除了体现 namespace 的概念外,从上面代码中我们还可以看到在两个 module 中定义的 method 和调用这个 method 时都在其前面加上其所在 module 的名字了。这样定义的函数叫 module method 。但是不是所有要使用 module 中函数都要显式的加上 module 名字呢?答案:不是。请往下看。

 

Mix-in Another wonderful use of “module”

module method 的定义和调用方式让我们联想到 class method (注意不是 class instance method ),也让我们从 module 联想到 class 了。但是 A module 是不能像 class 那样拥有实例的, module 毕竟还不是 class ,但是我们可以在 class 的定义中“ include ”一个 module ,这样会发生什么呢?

 

//considering the following code:

module Mammal  # 哺乳动物

         def suckle   # 哺乳

                   print "I can suckle my baby" ,"/n"

         end

end

 

module Flyable   # 可飞行的

         def fly        # 飞行         

                   print "I can fly", "/n"

         end

end

 

class Chiropter    # 蝙蝠

         include Mammal          # 蝙蝠是哺乳动物

         include Flying               # 蝙蝠可以飞行

end

 

aChiropter = Chiropter.new

aChiropter.suckle

aChiropter.fly

 

//output

>ruby TestMixin.rb

I can suckle my baby

I can fly

>Exit code: 0

 

如上面代码得出的结果我们可以看出,一旦一个 class include 了一个 module, 那么所有 module 中的 methods 就好像成为 class’s instance methods 一样,这就是 mix-in ,看起来被 mix-in modules 的行为更像是这个 class super class ,通过这种方法我们可以间接实现多重继承。但是注意看看被 mixin module 中的函数是如何定义的?和前面代表 namespace 概念的 module 中的 method 定义不同的是在于定义时 method name 前不见了 module name ,这样定义出来的 method module instance method , 虽然 module 不能实例化,但是一旦被 mixin 到某个 class 中,这些 module instance method 的使用就等价于 class instance method 了。

 

总结:

1.         Module 的两种用法

Ø         体现 namespace 概念;

Ø         mixin class 中间接的实现类的多重继承。

2.         Module 中的两种 method 定义和用法

module module_name

       def module_name. module_method_name

              # module method , for representing namespace concept

       end

 

       def module_instance_method_name

              # module instance method

              # Once the module has been mixed in,

#the method will be equal to a class instance method

       end

end

在编程中,**Mixin** 是一种设计模式,用于实现代码的复用和组合。其核心思想是通过将某些功能或行为定义在独立的模块中,再将其“混合”到其他类或组件中,从而避免继承的复杂性和限制。Mixin 的使用方式和具体实现会因编程语言或框架的不同而有所差异。 ### 面向对象编程中的 Mixin 在面向对象编程中,Mixin 提供了一种灵活的方式来扩展类的功能,而不依赖传统的单继承机制。Mixin 类通常包含一些特定的功能,这些功能可以被多个子类复用。例如,在 Python 中,可以通过多继承的方式将多个 Mixin 类组合到一个主类中,从而为该类添加多个独立的功能。这种方式避免了类层次结构的复杂化,同时提高了代码的可维护性 [^2]。 ### Vue.js 中的 Mixin 在 Vue.js 框架中,Mixin 是一种分发组件可复用功能的机制。Vue 的 Mixin 对象可以包含组件的任意选项,例如 `data`、`methods`、`created` 生命周期钩子等。当一个组件使用 Mixin 时,所有 Mixin 中定义的选项会被“混合”到组件自身的选项中。如果组件和 Mixin 中存在同名的选项,组件自身的选项会优先。这种方式非常适合在多个组件之间共享通用逻辑,例如数据处理、事件监听等 [^1]。 以下是一个 Vue2 中使用 Mixin 的示例: ```javascript // 定义一个 mixin 对象 const myMixin = { created() { this.hello(); }, methods: { hello() { console.log('Hello from mixin!'); } } }; // 在组件中使用 mixin export default { mixins: [myMixin], created() { console.log('Component created'); } }; ``` ### CSS 预处理器中的 Mixin 在 CSS 预处理器(如 Sass)中,**@mixin** 是一种用于定义可复用样式的机制。通过 `@mixin`,可以定义一组样式规则,并在多个地方通过 `@include` 调用,从而避免重复代码。Sass 的 Mixin 支持参数传递,使其更加灵活。例如,可以定义一个按钮样式 Mixin,并根据不同的参数生成不同样式的按钮 [^3]。 以下是一个 Sass Mixin 的示例: ```scss // 定义一个 mixin @mixin button-style($color, $radius: 5px) { background-color: $color; border-radius: $radius; padding: 10px 20px; color: white; border: none; cursor: pointer; } // 使用 mixin .primary-button { @include button-style(blue); } .secondary-button { @include button-style(green, 10px); } ``` ### 总结 Mixin 在不同上下文中的核心思想是一致的:通过组合而非继承的方式,实现功能的复用和扩展。无论是面向对象编程中的类组合,Vue.js 中的组件逻辑共享,还是 Sass 中的样式复用,Mixin 都提供了一种轻量级且灵活的解决方案。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值