design-patterns - Anti-pattern name for firing events on an object member supplied at instantiation?
问题描述
An object requires it be instantiated with an object upon which it can call a method to register than an event has occurred — rather than the instantiating code listening for events.
Is this an anti-pattern? If so, does it have a name?
Is there a reason to code this way?
解决方案
Passing an object-to-be-called into a constructor is usually used for the "constructor injection" type of dependency injection. See: https://en.wikipedia.org/wiki/Dependency_injection#Constructor_injection
When used in that way it's an excellent pattern...
But if the object being passed in is really an event listener, then it becomes a bad idea. Injecting an event listener is not "dependency injection", because an object doesn't depend on its event listeners, and so it shouldn't require any in its constructor. It would also be very bad form to restrict an event-generating object such that it could have only one listener.
推荐阅读
- python - .ppm 文件每 3 个像素分隔一次
- tensorflow - 张量流中的自定义层以输出其输入的运行最大值
- bazel - git repo 中非 bazel 项目的 Bazel 规则
- php - 使用 laravel 8 上传文件时不允许进行 Chmod() 操作
- google-apps-script - On[Event] Google Apps 脚本挂钩
- c# - 使用@Html.ActionLink 将参数传递给Controller
- r - 'data' 必须是二维的(例如数据框或矩阵)
- keyboard-shortcuts - 在 iTerm2 中执行 shell 命令的键盘快捷键
- java - 从另一个目录中的另一个源文件导入一个类
- cocoa - 我正在 macOS 上尝试 GCDAsyncSocket 以获取特定网络接口的外部 IP。这不适用于网络扩展 VPN。任何想法?