在我的java模型中管理双向关联 [英] Managing bidirectional associations in my java model

查看:245
本文介绍了在我的java模型中管理双向关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java模型,其中类具有关联(1到1,1到n,n到n),它们应该在两个方向上工作。

I have a java model where the classes have associations (1 to 1, 1 to n, n to n) that should work in both directions.

示例:
A类有一个B类集合

Example: class A has a collection of class B

B的所有实例必须知道,他们的父母(A)是谁。如果我更改了B的父属性,那么关联也需要在另一方更新。

All instances of B must know, who their parent (A) is. If I change the parent property of B, the association needs to be updated on the other side as well.

我认为,编写代码可以使关联保持最新在两端是相当繁琐和错误的。

I think, writing code, that keeps the associations up to date on both ends is quite tedious and errorprone.

所以:有更好的方法吗?是否有一个可以管理两个对象之间的双向关联的java库?

So: Is there a better way of doing this? Is there a java library that can manage bidirectional associations between two objects?

推荐答案

另一种方法是将关系移出对象有问题。很多时候A和B都不需要彼此了解;使用代码可以方便地找到这些属性。这是图形或双向地图可以发挥作用的地方。

Another approach is to move the relationship out of the objects in question. Many times neither A nor B ever needs to know about each other; it's the using code that finds those properties convenient. This is where graphs or bidirectional maps can come into play.

双向地图可以简单地用于跟踪一对一的关系。图表可以更好地跟踪其他类型的基数(多对一,多对多等)。

Bidirectional maps can trivially be used to keep track of one to one relationships. Graphs can be much better at keeping track of the other types of cardinality (many-to-one, many-to-many, etc.)

有几个不同图表实现可以帮助实现这一目标。我听说但从未使用过JGraphT,而且我使用了很多名为plexus(与IOC容器无关)。 http://jgrapht.sourceforge.net/ http://plexus.sf.net/

There are a couple different graph implementations that could help on that end. I've heard of but never used JGraphT and there is one I've used a lot called plexus (no relation to the IOC container). http://jgrapht.sourceforge.net/ and http://plexus.sf.net/ respectively.

图表很好,因为它允许完全灵活如何定义不同的关系,隐式维护双向链接。

A graph is nice because it allows complete flexibility in how different relationships are defined and the bidirectional link is maintained implicitly.

关系双方需要保持同步的事实往往表明关系本身与端点同等重要,而不是每一方都应该试图封装的东西。

The fact that both sides of the relationship need to keep themselves in synch is frequently a sign that the relationship itself is of equal importance to the endpoints and not something that each side should be attempting to encapsulate.

但是,如果父母和孩子真的需要为了相互操作,一种方法是找出哪个是主要的,以及是否所有操作都可以通过该对象完成。因此,例如,在父子关系中,可以对父进行子操作,父进程在该操作期间向子进程传递对其自身的引用。我认为如果你不能那么它是一个很好的指标,在​​设计中需要重新绘制一些行。

Still, if parent and child really do need to operate on each other then one approach is to figure out which is primary and whether all operations can be done through that object. So, for example, in the parent child relationship can child operations be done on the parent with the parent passing a reference to itself to the children during that operation. I'd argue that if you can't then it is a good indicator that some lines need to be redrawn in the design.

再次使用父子示例,我还没有找到一个案例,其中parent-> child和child-> parent关系是如此动态,以至于一端无法控制它。 99%的时间我保持从次要到初级的反向引用,这是为了方便,并且关系的生命周期已经确定。

Again using the parent-child example, I haven't really found a case where the parent->child and child->parent relationship was so dynamic that one end couldn't control it. And 99% of the time that I keep a back-reference from secondary to primary, it's for convenience and the lifecycle of the relationship is well established.

...否则,我用图表。

...otherwise, I use a graph.

这篇关于在我的java模型中管理双向关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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