编程一对多关系 [英] Programming a one-to-many relationship

查看:127
本文介绍了编程一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我很惊讶,在google和stackoverflow上进行搜索并没有返回更多的结果。

So I am surprised that doing a search on google and stackoverflow doesn't return more results.

在OO编程(我使用的是java)中,怎么做你正确地实现一对多关系?

In OO programming (I'm using java), how do you correctly implement a one-to-many relationship?

我有一个类客户和类作业。我的申请是为一个虚构的公司,为客户完成工作。我目前的实现是这样的: Job 类与 Customer 类没有任何关系,没有参考它。 客户类使用集合和方法来保存,检索和修改由客户分配和/或完成的作业的信息。

I have a class Customer and class Job. My application is for a fictious company that completes jobs for customers. My current implementation is so that the Job class doesn't have anything to do with the Customer class, there is no reference to it at all. The Customer class uses a collection and methods to hold, retrieve and modify information about the Jobs that have been assigned by and/or completed for a customer.

问题是,如果我想了解哪个客户特定的 Job 已经完成了什么?我只发现这篇文章是相关的: http:/ /www.ibm.com/developerworks/cn/webservices/library/ws-tip-objrel3/index.html

The question is, what if I'd want to find out for which customer a particular Job has been done? I've only found this article that's relevant: http://www.ibm.com/developerworks/webservices/library/ws-tip-objrel3/index.html.

根据作者的实现,我将让作业构造函数采用客户参数,并存储它,以便我可以检索它。但是,我并不保证这种模式可以一致。没有任何重新设置将相关客户作为作业的客户设置为该作业,而向为其他人完成的客户添加作业。

According to the implementation of the author, I would let the Job constructor take a Customer parameter, and store it so I can retrieve it. However, I see no guarantee at all that this model can be consistent. There are no restirctions to set the related customer for a job as a customer that the job was not for, and add jobs to customers that were done for someone else. Any help on this would be appreciated.

推荐答案

没有100%的确保方式来维护诚信。

There's no 100% surefire way to maintain the integrity.

通常采用的方法是使用一种方法来构建关系,并以相同的方法构建另一个方向。但是,正如你所说,这不会阻止任何人混淆。

The approach which is usually taken is to use one method to construct the relationship, and construct the other direction in that same method. But, as you say, this doesn't keep anyone from messing with it.

下一步将是使一些方法可以访问,以便在与您无关的最少代码无法打破:

The next step would be to make some of the methods package-accessible, so that at least code which has nothing to do with yours can't break it:

class Parent {

  private Collection<Child> children;

  //note the default accessibility modifiers
  void addChild(Child) {
    children.add(child);
  }

  void removeChild(Child) {
    children.remove(child);
  }
}

class Child {

   private Parent parent;
   public void setParent(Parent parent){
     if (this.parent != null)
       this.parent.removeChild(this);
     this.parent = parent;
     this.parent.addChild(this);
   }
}

在现实中,你不会经常模拟这种关系在你的班上相反,您将在某种存储库中查找父母的所有子项。

In reality, you won't often model this relationship in your classes. Instead, you will look up all children for a parent in some kind of repository.

这篇关于编程一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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