这是否被视为周期性依赖关系这是一种好习惯吗?

如何解决这是否被视为周期性依赖关系这是一种好习惯吗?

嗨,我是一名新的Java程序员,对类设计有一个小问题。

我知道类似这样的事情是周期性的,可能不是构造项目的一种方式:

public class Course {
    private ArrayList<Student> students;

    public Course (ArrayList<Student> students) {
        this.students = students;
    }
}


public class Student {
    private Course course;

    public Student (Course course) {
        this.course = course;
    }
}

但是如果将Student.java更改为:

public class Student {
    private int courseId;

    public Student (int courseId) {
        this.courseId = courseId;
    }
}

,因此courseId可用于从DAO或其他内容中检索课程。这仍然是一个好的结构吗?从现在开始,每门课程仍然关心学生。而且每个学生仍然关心课程。

解决方法

倒数引用很好,所有数据库实体都并排引用。

构造函数必须允许创建,而不能禁止只能在其他已经存在的情况下创建它们。

因此可能有两个构造函数。

public class Course {
    private List<Student> students = new ArrayList<>();

    public Course() {
    }

    public Course (List<Student> students) {
        this.students.addAll(students);
    }
}


public class Student {
    private Course course;

    public Student (Course course) {
        this.course = course;
        course.addStudent(this);
    }
}

在数据库中,一个经常具有数字ID,但是许多对象/关系映射仍然可以使用ID(JPA,eclipseLink,hibernate)在后台实现上述类。不需要具有非对称ID和对象引用。

您不应使用具体的实现(ArrayList),而应使用最灵活的方法(List)。

另外,最好不要公开字段的内部数据(students)以供外部更改。


关于归纳(List)和实现(ArrayList

某些(脚本)语言的集合只有一种类型。 Java是 设计用于为一个接口提供多种实现。

因此,您可以决定是将Map的数据结构实现为快速HashMap还是有序TreeMap。通常,用户现在只需要了解Map。您不需要过度说明代码,甚至可以使用其他实现类来重新设计。

List<String> list = ...
Collections.sort(list);
list.add(...);

List<String> convert(List<String> list) { ... }

在您的convert上方的代码中,可以处理任何类型的List;您不仅为ArrayList编写了更通用的算法。在该方法中,您可以返回ArrayList或LinkedList。这样一来,就不会因错误的规范过度而更改代码。

Coding Against Interfaces的尼克·霍奇斯(原谅错误的电源插座和TypeScript)。

,

第一个代码段可以被认为是有效的设计(需进行一些小的修改,请参见下文)。假设您有一所学院:

  • 要注册Student,学生需要选择一门课程,因此构造者public Student (Course course)
  • 并且Course包含一个已注册学生的列表,但最初创建时没有学生。它们会在以后添加。

实际上我可以这样使用它:

public class Course {
    private String name;
    private List<Student> students;

    public Course (String name) {
        this.name = name;
        students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }
}


public class Student {
    private Course course;

    public Student (Course course) {
        this.course = course;
    }
}

/// -- now...

Course history = new Course("History");
Student historyStudent = new Student(history);
history.add(historyStudent);

请注意,这种设计可能有多种变体,但我的观点是,您的设计几乎是有效的,只是做了一些小的改动,即不设置课程初始化时的学生名单。

,

老实说,您最好以DAO / POJO的方式进行此操作,并将课程与学生脱钩

如果您使用的是SQL数据库,则可能会设置类似

的表
Course
Student
Enrollment

注册包含课程,学生,开始日期和结束日期的地方。这样,课程和学生就可以解耦了。

我建议您像这样对课程建模

,

在模型中,尤其是由数据库支持的模型中,这种类型的循环依赖性不被认为是不好的。实际上,这在ORM中很常见(可能在所有Java项目的95%中,您都会看到类似的东西。)

另一方面,带有courseId的选项是一个更糟的主意:它将模型的技术方面(如果要反映一些代理主键)泄漏到Java域中。

标准免责声明:基于几个摘要将软件体系结构称为“好”或“差”,就像判断基于砖的建筑体系一样。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-