造型带类的父子关系 [英] Modelling Parent-Child Relationships with Classes

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

问题描述

我工作的一个轻量级的文件管理系统和正在寻找一些帮助,如何最好地某些关系进行建模。从本质上讲,我与两个组织单位的工作:键入秒。当一群和类型相结合便形成了链接文件是那么相关。请注意,可与多个类型的结合,因此,例如,你可以有一个由链接的'第1组和A类和第二链接组成的1组和B类。在我看来,这是不是真的有结构性的最佳途径,但在这一点上我无法改变它,所以我必须凑合着用,我给什么。

I am working on a lightweight document management system and am looking for some help with how best to model certain relationships. Essentially, I am working with two "organizational units": Groups and Types. When a group and a type are combined they form a Link with which Documents are then associated. Note that a Group can be combined with more than one type, so for example you can have one Link composed of 'Group 1' and 'Type A' and a second Link composed of 'Group 1' and 'Type B'. In my opinion this is not really the best way to have structured it but at this point I'm unable to alter it so I have to make do with what I'm given.

A 文件可以是多个链接 s的成员。因此,例如,文档A'可以是'链接10和链接13'的成员。

A Document can be a member of multiple Links. So for example, 'Document A' can be a member of 'Link 10' and 'Link 13'.

我的问题是,有时我会想显示单文件键,列表中的每个链接文件所属,和有时我会想显示一个链接并列出每个文件属于该链接

My issue is that sometimes I will want to display a single Document and list every Link that Document belongs to, and other times I will want to display a single Link and list every Document that belongs to that Link.

我不知道如何表达这些类之间的关系。我看着Composite模式,但我不认为这会为我工作,因为它似乎需要一个孩子只有一个父母,在我的情况,其中一个孩子可以有多个父母。任何帮助,将不胜感激。

I'm not sure how to represent the relationship between these classes. I've looked into the Composite Pattern but I don't think this will work for me because it seems to require that a child have only one parent, where in my case a child can have multiple parents. Any help would be appreciated.

推荐答案

什么你可能寻找是N对M映射。是所有OOP-Y,您可以存储每类中的这些映射:

What you're probably looking for is an n-to-m mapping. To be all OOP-y, you can store these mappings within each class:

// actually write it using properties, information hiding etc instead...
public class Document {
    public ICollection<Link> Links;
}

public class Link {
    public ICollection<Document> Documents;

    // this can be on Document as well, depending on what semantics you want...
    public void Add(Document d) {
        Documents.Add(d);
        d.Links.Add(this);
}

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

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