Java 中的组合与双向关联

发布于 2024-10-06 09:53:47 字数 43 浏览 1 评论 0原文

理论上的差异我理解,但是代码实现上有什么差异呢?有人可以提供一些例子吗?

I understand the differences in theory, but what is the difference in the code implementation? Can somebody provide some examples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的故事 2024-10-13 09:53:48

实际上,组合是单向关联——除了语义上,我们将其解释为“该事物是该事物的一部分”,而不是简单地“该事物持有对该事物的引用”。

Composition is, in effect, uni-directional association - except that semantically, we interpret it as meaning "that thing is part of this thing" rather than simply "this thing holds a reference to that thing".

满栀 2024-10-13 09:53:47

我们有学生和大学的目的

class University {
    private final Set<Student> students = new HashSet<Student>();
    void addStudent(Student s){students.add(s);}
}

class Student {
    private final String name;
    public Student(String name) {
        this.name = name;
    }
}

我们以某种方式创造这些东西

University university = new University();
Student bob = new Student("Bob");
university.addStudent(bob);

并且知道我们需要知道鲍勃在大学学习。
因此,我们为大学创建了一些新方法

boolean contains(Student student){
        for(Student s : students){
            if(s.equals(student)) return true;
        }
        return false;
    }

,而不是像 university.contains(bob) 这样的 smt。

但如果我们没有与大学联系会怎样?我们需要问问鲍勃。但鲍勃不知道。所以我们从组合转向双向并创建 smt 像

class University {
    private final Set<Student> students = new HashSet<Student>();
    void addStudent(Student s){
        students.add(s);
        s.setUniversity(this);
    }
    boolean contains(Student student){
        for(Student s : students){
            if(s.equals(student)) return true;
        }
        return false;
    }
}

class Student {
    private final String name;
    private University university;
    public Student(String name) {
        this.name = name;
    }
    void setUniversity(University u){
        university = u;
    }
    boolean doYouStudyInUniversity(){
        return university != null;
    }
}
//ask
bob.doYouStudyInUniversity();

Purpose we have students and universities

class University {
    private final Set<Student> students = new HashSet<Student>();
    void addStudent(Student s){students.add(s);}
}

class Student {
    private final String name;
    public Student(String name) {
        this.name = name;
    }
}

We create this stuff in some way

University university = new University();
Student bob = new Student("Bob");
university.addStudent(bob);

And know we need to know does Bob studies in university.
So we create some new method for university

boolean contains(Student student){
        for(Student s : students){
            if(s.equals(student)) return true;
        }
        return false;
    }

and, than do smt like university.contains(bob).

But what will be if we havent link to uniwersity. We need to ask it Bob. But Bob doesn't know. So we go from composition to bi-derection and create smt like

class University {
    private final Set<Student> students = new HashSet<Student>();
    void addStudent(Student s){
        students.add(s);
        s.setUniversity(this);
    }
    boolean contains(Student student){
        for(Student s : students){
            if(s.equals(student)) return true;
        }
        return false;
    }
}

class Student {
    private final String name;
    private University university;
    public Student(String name) {
        this.name = name;
    }
    void setUniversity(University u){
        university = u;
    }
    boolean doYouStudyInUniversity(){
        return university != null;
    }
}
//ask
bob.doYouStudyInUniversity();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文