gpt4 book ai didi

java - OOP - 向类添加属性的最佳方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:55:27 25 4
gpt4 key购买 nike

我们开发了一个用 Java 编写并映射到 Hibernate 的 HR 应用程序;特征之一是招募阶段。

Candidate 类的建模如下:

public class Candidate  {
private String id;
private Integer candidateCode;
private GregorianCalendar birthDate;
private String italianFiscalCode; //unique code for italian people
}

由于我们只是为市场开发,到目前为止代码非常依赖于特定的立法,请查看 fiscalCode 类属性。

要求我们推广这个概念,以便能够扩展到其他市场,例如,唯一标识符可以不同,可以由多个字符串组成,或者根本不存在。

我的第一 react 是:

1 - 只需将该字段重命名为 countryIdentifier 并根据特定国家/地区的需要添加其他字段。

private String countryIdentifier; //general unique code
private Integer greekAddedCode;

这意味着在需要的地方重构代码(所有使用旧 italianFiscalCode 的地方),重命名 DBMS 列(并最终添加其他列)并修改使用该字段的所有查询。

这对我来说是一个糟糕的实现

2 - 子类 Candidate 创建 ItalianCandidateGreekCandidate 并在子类中移动特定字段。

问题是 Candidate 类已经被 HeavyCandidate 子类化了,它具有优化 Hibernate 映射的唯一功能,因为我们移动了所有“重”属性(许多-to-ones and sets) 在重类中(这是我们所有 bean 都遵循的方法)。

在这种情况下,最正确的做法是什么?

最佳答案

我会创建一个接口(interface) Identifier(不确定名称),它由 GreekIdentifierItalianIdentifier 等类实现。然后我会向 Candidate 添加一个字段:

Identifier identifier;

GreekIdentifier 的实现看起来像这样:

public class GreekIdentifier implements Identifier {
String countryIdentifier;
int addedCode;

//constructor, getters, setters ...
//actual behaviour, Indentifier @Overrides ...
}

如果 countryIdentifier 确实是所有标识符都具有的东西,您甚至可以将其向上移动到(抽象)基类。

关于java - OOP - 向类添加属性的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40709796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com