ruby继承vs mixins [英] ruby inheritance vs mixins

查看:96
本文介绍了ruby继承vs mixins的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby中,由于你可以包含多个mixin,而只能扩展一个类,因此看起来mixin比继承更受欢迎。

In Ruby, since you can include multiple mixins but only extend one class, it seems like mixins would be preferred over inheritance.

我的问题:编写代码,必须扩展/包含有用,为什么你会做一个类吗?或者换句话说,为什么你不总是把它作为一个模块?

My question: if you're writing code which must be extended/included to be useful, why would you ever make it a class? Or put another way, why wouldn't you always make it a module?

我只能想到一个原因,你想要一个类,这是if你需要实例化类。然而,在ActiveRecord :: Base的情况下,您从不直接实例化它。

I can only think of one reason why you'd want a class, and that is if you need to instantiate the class. In the case of ActiveRecord::Base, however, you never instantiate it directly. So shouldn't it have been a module instead?

推荐答案

我只是阅读这个主题 井井有条的Rubyist (很棒的书,顺便说一句)。作者比我更好地解释,我会引用他:

I just read about this topic in The Well-Grounded Rubyist (great book, by the way). The author does a better job of explaining than I would so I'll quote him:

没有单一规则或公式产生正确的设计。但是,在进行类与模块决策时,请记住
的几个注意事项:

No single rule or formula always results in the right design. But it’s useful to keep a couple of considerations in mind when you’re making class-versus-module decisions:


  • 模块没有实例。因此,实体或东西通常最好是在类中建模的
    ,实体或东西的特性或属性是
    最好封装在模块中。相应地,如4.1.1节所述,类
    名称往往是名词,而模块名称通常是形容词(Stack
    和Stacklike)。

  • Modules don’t have instances. It follows that entities or things are generally best modeled in classes, and characteristics or properties of entities or things are best encapsulated in modules. Correspondingly, as noted in section 4.1.1, class names tend to be nouns, whereas module names are often adjectives (Stack versus Stacklike).

类只能有一个超类,但它可以根据需要混合多个模块。如果
你使用继承,优先创建一个明智的超类/子类
关系。不要用掉一个类的唯一超类与
的关系,让类只能是几组特征中的一个。

A class can have only one superclass, but it can mix in as many modules as it wants. If you’re using inheritance, give priority to creating a sensible superclass/subclass relationship. Don’t use up a class’s one and only superclass relationship to endow the class with what might turn out to be just one of several sets of characteristics.

在一个例子中总结这些规则,这里是你不应该做的:

Summing up these rules in one example, here is what you should not do:

module Vehicle 
... 
class SelfPropelling 
... 
class Truck < SelfPropelling 
  include Vehicle 
... 

module SelfPropelling 
... 
class Vehicle 
  include SelfPropelling 
... 
class Truck < Vehicle 
... 

第二个版本对实体和属性建模更加整齐。卡车
从车辆(这是有道理的)下降,而自推进是车辆的特征(至少,我们关心的这个世界的模型) - 一个特征,通过卡车传递到卡车作为车辆的后代,或专门的
形式。

The second version models the entities and properties much more neatly. Truck descends from Vehicle (which makes sense), whereas SelfPropelling is a characteristic of vehicles (at least, all those we care about in this model of the world)—a characteristic that is passed on to trucks by virtue of Truck being a descendant, or specialized form, of Vehicle.

这篇关于ruby继承vs mixins的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆