陷入一对多关系的循环依赖
所以我正在构建一个系统,其中有一个服务器对象,它生成上传器对象。两者都被定义为协议/接口,无论您喜欢哪个术语。 Server 对象有一个生成 Uploader 并返回它的方法,其签名如下:
- (id
Uploader 需要包含对创建它的 Server 的引用,因为它需要对服务器的引用才能从我的钥匙串包装器获取密码。因此,它包含一个返回其父服务器的方法:
- (id
当然,这会在两个协议之间创建循环依赖关系。关于如何解决这个问题有什么想法吗?
谢谢!
比利
So I'm building a system in which there is a server object, and it generates Uploader objects. Both are defined as protocols/interfaces, whichever term you prefer. The Server object has a method which generates an Uploader and returns it, with the following signature:
- (id<Uploader>)generateUploader;
The Uploader needs to contain a reference back to the Server which created it, because it needs a reference to a Server to get the password from my keychain wrapper. So, it contains a method which returns its parent Server:
- (id<VayprServer>)parentServer;
Of course, this creates a circular dependency between the two protocols. Any ideas on how to fix this?
Thanks!
Billy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要打破依赖关系,就像所有循环依赖关系一样,您必须在 .hs 中前向声明内容。特别是:
and
和
and
这样,两个 .ms 将看到以正确顺序声明的内容。
To break the dependency, like all circular dependencies, you gotta forward-declare stuff in the .hs. In particular:
and
and
and
This way, the two .ms will see things declared in the correct order.
这不一定是反模式。
在树结构(例如 Windows 资源管理器中的资源管理器树)中,树公开了节点的集合,但每个节点都有对树的引用。
This is not necessarily an anti-pattern.
In a tree structure such as the Explorer Tree in Windows Explorer, the Tree exposes a collection of Nodes, but each Node has a reference to the Tree.