Smalltalk 中的单一职责

发布于 2024-10-15 16:20:33 字数 635 浏览 3 评论 0原文

如果单一职责原则适用于OOP,并且smalltalk(以及ruby)被视为一个大多数OO语言中为什么Object类可以响应这么多消息?

仅来自Object methodDict explore的一些内容:

  • 检查、探索、浏览、打印:on:
  • accept(所有对象上的访问者模式?)
  • copy、deepCopy、join、joinTo、at:、at:modify:
  • asString ,asFunction,asOrderedCollection(为什么不也是asSet?)
  • 海边的:asLink,asJson,asJavascript

这不是对象的责任(例如用户域模型应该只对其私人消息,付款等感兴趣)

编辑:< /strong> 其中一些是有意义的(asString、asOrderedCollection、accept、notify),而另一些则看起来很奇怪(at:、asFunction、deepCopy、join、joinTo)

If the Single Responsibility Principle applies to OOP and smalltalk (&ruby as well) is considered as one of the most OO languages why can Object class respond to so many messages?

Just a few from Object methodDict explore:

  • inspect, explore, browse, print: on:
  • accept (visitor pattern on all objects?)
  • copy, deepCopy, join, joinTo, at:, at: modify:
  • asString, asFunction, asOrderedCollection (why not asSet also?)
  • seaside ones: asLink, asJson, asJavascript

It is not object's responsibility (for example user domain model should be interested only in its private messages, payments, etc.)

EDIT: some of them are meaningful (asString, asOrderedCollection, accept, notify) while others seems pretty weird (at:, asFunction, deepCopy, join, joinTo)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转瞬即逝 2024-10-22 16:20:33

您必须考虑 Smalltalk 的模块化功能。也就是说,方法定义独立于类定义,因此Object 上的方法可以与它们相关的应用程序打包(例如,Seaside)。这些扩展方法不是基本系统的一部分,因此它们只是从它们所属的包的角度向它们的类添加职责。其中许多方法都是简单的双调度点:如果 anObject asFoo 只是委托给 Foo fromObject: anObject,我不会说它给类增加了太多责任。

inspectcopydeepCopy 这样的反射方法在概念上在 Object 上占有一席之地,但我同意有更好的方法反射架构(镜像)。

现在,Smalltalk 可能是一个具有美丽原则的理想选择,但您必须对特定的实现持保留态度:)

Smalltalk 系统有发展成为大型整体系统的趋势,因为更改基本系统非常容易,而且因为它人们倾向于将映像用作开发工件,从而绕过持续集成的良好实践。最后,许多这些有趣的班级责任都是由于历史/现实原因造成的; Squeak 尤其如此,因为它主要是作为快速多媒体实验的平台而开发的,而不是用于软件工程教育或行业目的(Pharo 的目标是)。

You have to consider the modularity features of Smalltalk. Namely, method definitions are independent from class definitions, so methods on Object can be packaged with the application they relate to (e.g., Seaside). These extension methods are not part of the base system, so they only add responsibilities to their class from the point of view of the package they belong to. Many of these methods are simple double-dispatch points: if anObject asFoo simply delegates to Foo fromObject: anObject, I wouldn't say it adds much responsibility to the class.

Reflective methods like inspect, copy, deepCopy conceptually have their place on Object, but I agree that there are better architectures for reflection (Mirrors).

Now, Smalltalk might be an ideal with beautiful principles, but you have to take particular implementations with a grain of salt :)

Smalltalk systems have a tendency to evolve into big monolithic systems, because it's so easy to change the base system, and because it's tempting to use the image as a development artifact, bypassing good practices of continuous integration. In the end, a lot of these funny class responsibilities are due to historical/practical reasons; this is especially true of Squeak, because it was primarily developed as a platform for fast multimedia experimentation, rather than for software engineering education or industry purposes (which Pharo aims to be).

当梦初醒 2024-10-22 16:20:33

有几个原因:

  • Object 和 ProtoObject 是 Smalltalk 对象模型的基础,因此它们承担重大责任是正常的,例如负责复制、序列化等。
  • 得墨忒耳定律在这里可能更重要:如果不是对象本身,谁将负责这些功能?
  • 其中许多函数用于调试和表示目的。您无需浏览、探索和检查即可工作(但它们确实很有用)。

本质上,所有这些消息代表了对象可以执行的所有操作,并且您可以执行很多操作。

A few reasons:

  • Object and ProtoObject are the basis of Smalltalk's object model, so it's normal that they have big responsibilities, like taking care of copy, serialization, etc.
  • The law of Demeter may be more important here: who will take care of these functions, if not the Object itself?
  • A lot of these functions are there for debugging and representation purposes. You could work without browse, explore and inspect (but they're really useful).

Essentially, all these messages represent everything an object can do, and there's a lot of things you can do.

静谧 2024-10-22 16:20:33

因为在 Smalltalk 中 - 一切都是一个对象,并且根 Object 类本质上必须管理所有系统行为,包括类层次结构(因为所有类都是对象)和元类层次结构。

权力越大,责任越大。

在其他系统中,对象只是整个系统的子域,对象不必做这么多,因此不需要被赋予这么多命名的职责。

Because in Smalltalk - everything is an object, and the root Object class essentially has to govern all system behaviour, including the Class hierarchy (as all Classes are objects) and the Metaclass hierarchy.

With great power comes great responsibility.

In other systems, where the objects are simply a subdomain of the entire system, Object does not have to do so much, and so does not need to be given so many named responsibilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文