热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Objective-C类别与Mixins相比-Objective-CcategorycomparedtoMixins

IstheconceptoftheObjective-Ccategoriesinanywaysimilartotheconceptofmixins?Ifso:what

Is the concept of the Objective-C categories in anyway similar to the concept of mixins? If so: what are the similarities? In not: what are the differences?

Objective-C类别的概念是否与mixins的概念类似?如果是这样的话:有什么相似之处?不是:有什么区别?

4 个解决方案

#1


17  

To the best of my understanding:

据我所知:

Mixins

  • Syntactic sugar for composition
  • 组成的句法糖
  • Added by the developer of the class, not the user
  • 由类的开发人员添加,而不是用户
  • Can be reused by multiple classes
  • 可以由多个类重用
  • Can add instance variables
  • 可以添加实例变量
  • Can be implemented using forwarding in Objective-C
  • 可以使用Objective-C中的转发来实现

Categories

  • Similar to extension methods in other languages
  • 与其他语言的扩展方法类似
  • Usually added by the user of the class, not the developer
  • 通常由类的用户添加,而不是开发人员
  • Used by exactly one class and its subclasses
  • 仅由一个类及其子类使用
  • Can't add instance variables
  • 无法添加实例变量

#2


7  

To be clear the answer is NO - they are not the same.

要明确答案是否定的 - 它们不一样。

The differences are outlined by John Calsbeek in the accepted answer, but I would say the key difference is the one where mixins can be used in different classes, whereas categories always extend exactly one class - which they declare in their definition.

John Calsbeek在接受的答案中概述了这些差异,但我要说的主要区别在于mixins可以在不同的类中使用,而类别总是只扩展一个类 - 他们在定义中声明了这一类。

This is the key difference because it means the use cases for these two features are utterly different. Another way of looking at it is that, if you're coming from Ruby to Objective-C and missing your mixins, you won't find any joy with categories.

这是关键的区别,因为这意味着这两个功能的用例完全不同。另一种看待它的方式是,如果你从Ruby到Objective-C并且缺少你的mixins,你就不会对类别感到高兴。

The use case for mixins is that you have some code - methods and instance variables - that you want to reuse in several classes that don't have a common superclass. You can't do that with categories.

mixins的用例是你有一些代码 - 方法和实例变量 - 你想在几个没有公共超类的类中重用它们。你不能用类别做到这一点。

Mixins are effectively "multiple-inheritance" of the type you don't find in Objective-C. The closest thing in objective-c is protocols, just as the closest thing Java is interfaces, but they have neither instance variables nor method bodies (in objective-C or java). So you're generally left with creating helper classes or putting code in superclasses.

Mixins实际上是在Objective-C中找不到的类型的“多重继承”。 objective-c中最接近的是协议,就像Java最接近接口一样,但它们既没有实例变量也没有方法体(在Objective-C或java中)。因此,您通常只需要创建辅助类或将代码放在超类中。

The use case for objective-c categories is that you want to add methods to an existing class - even a system or library class.

objective-c类别的用例是您希望将方法添加到现有类 - 甚至是系统或库类。

I would say that mixins are more powerful, but since it's an apples-to-oranges comparison, it would be pointless.

我会说mixins更强大,但由于它是一个苹果到橙子的比较,它将毫无意义。

To be accurate:

准确地说:

  • the Ruby equivalent of Categories, is to simply reopen the class you want to extend and extend it. (You can do that anywhere in Ruby, and it's effectively identical to Categories)

    类似于Ruby的Ruby,就是简单地重新打开要扩展和扩展它的类。 (你可以在Ruby的任何地方做到这一点,它实际上与类别相同)

  • I'm not sure what the objective-c equivalent to Mixins is though - anyone?

    我不确定与Mixins相当的Objective-c是什么 - 有人吗?

[Update] A bit more searching, and no there isn't an equivalent of Mixins in Objective-C, but the enterprising Vladimir Mitrovic has created a library that effectively does it. https://github.com/vl4dimir/ObjectiveMixin

[更新]更多的搜索,并没有在Objective-C中没有相当的Mixins,但是进取的弗拉基米尔米特罗维奇已经创建了一个有效地完成它的库。 https://github.com/vl4dimir/ObjectiveMixin

I'm in two minds as to whether to use it or not: sometimes if the language you're using doesn't support something, it's easier to work with it rather than fight it or try to import your favourite features from other languages. ("If you can't be with the programming language you love, love the one you're with").

关于是否使用它,我有两种想法:有时如果您使用的语言不支持某些内容,则更容易使用它而不是对抗它或尝试从其他语言导入您喜欢的功能。 (“如果你不能使用你喜欢的编程语言,那就爱上你所爱的人”)。

Then again, perhaps that's just a bit to snooty of me. The whole Aspect Oriented Programming movement has been glomming features onto Java for years (but never gaining much traction, I might add, outside of JBoss). Anyway, Vladimir gets extra kudos for using Ninja Turtles in his example.

然后,也许这只是对我的一点点嗤之以鼻。整个面向方面的编程运动多年来一直在将功能集中到Java上(但是我可能会在JBoss之外添加很多牵引力)。无论如何,弗拉基米尔在他的例子中获得了使用忍者龟的额外荣誉。

On another side node: as a relative objective-c noob, it seems to me that categories are way overused in sample code I find all over the web. It seems common practice to add static helper methods to system classes with categories, when it would be just as easy to create a helper class to house those methods in your project, with less risk of them breaking when the system class is updated or you import someone else's library with their own such categories. A common example is adding new static color methods to UIColor. Why not just add them to a local class?

在另一个节点上:作为一个相对的客观-c菜鸟,在我看来,我在整个网络上找到的示例代码中的类别被过度使用。通常的做法是将静态辅助方法添加到带有类别的系统类中,这样就可以很容易地创建一个辅助类来容纳项目中的那些方法,并且在更新系统类或导入时它们的风险会降低别人的图书馆有自己的类别。一个常见的例子是向UIColor添加新的静态颜色方法。为什么不将它们添加到本地类?

The one really good use I've seen for categories is adding methods, not to system classes, but to generated classes. So when you generate classes from your core-data object model, and you want to add new constructors or other methods that really do belong in the model class, you can do it with categories, allowing you to safely regenerate the model class if you change your model, without losing your work.

我在类别中看到的一个非常好的用法是添加方法,而不是系统类,而是生成类。因此,当您从核心数据对象模型生成类,并且想要添加新构造函数或其他真正属于模型类的方法时,您可以使用类别执行此操作,从而允许您在更改时安全地重新生成模型类你的模特,不会丢失你的工作。

In summary: - forget about categories as a solution for mixins - categories are good for core-data but overused and overrated otherwise

总结: - 忘记类别作为mixins的解决方案 - 类别对核心数据有利,但过度使用和高估

#3


4  

Categories are defined for a particular class, as far as I know, you can't create a category and add the methods it implements to several classes.

为特定类定义类别,据我所知,您无法创建类别并将其实现的方法添加到多个类中。

#4


2  

With a mixin, you might derive a new class from your base and the mixin, then instantiate this new class to take advantage of it.

使用mixin,您可以从您的base和mixin派生一个新类,然后实例化这个新类以利用它。

With a category, you are effectively adding directly the base class, so that all instances of that base have access to the functionality provided by the category.

使用类别,您可以直接有效地添加基类,以便该基础的所有实例都可以访问该类别提供的功能。


推荐阅读
author-avatar
dongquchunlaizv_123
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有