我们开发了一个用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
类属性。
请求是我们概括了这个概念,以便能够扩展到其他市场,例如唯一标识符可以不同,可以由几个字符串组成或根本不存在。
首先出现在我的脑海中:
1 - 只需将字段重命名为countryIdentifier,并根据特定国家/地区的需要添加其他字段。
private String countryIdentifier; //general unique code
private Integer greekAddedCode;
这意味着在需要的地方重构代码(所有放置旧的italianFiscalCode的位置),重命名DBMS列(并最终添加其他代码)并修改使用该字段的所有查询。
这对我来说看起来很糟糕
2 - 创建Candidate
和ItalianCandidate
的子类GreekCandidate
并移动子类中的特定字段。
问题是Candidate
类已经由HeavyCandidate
子类化,它具有优化Hibernate映射的唯一功能,因为我们移动所有“重”属性(多对一和多组) )在沉重的类中(这是我们跟随所有豆类的方法)。
在这种情况下,最正确的方法是什么?
答案 0 :(得分:1)
我会创建一个由GreekIdentifier
和ItalianIdentifier
等类实现的接口Candidate
(不确定名称)。然后我将一个字段添加到Identifier identifier;
:
GreekIdentifier
public class GreekIdentifier implements Identifier {
String countryIdentifier;
int addedCode;
//constructor, getters, setters ...
//actual behaviour, Indentifier @Overrides ...
}
的实现看起来像这样:
countryIdentifier
如果<?php
if(!($stmt = $mysqli->prepare("INSERT INTO Heroes(HeroName, FirstName, LastName, Age, HomeCountry, RoleID) VALUES (?,?,?,?,?,?)"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("sssisi",$_POST['HeroName'],$_POST['FirstName'],$_POST['LastName'],$_POST['Age'],$_POST['HomeCountry'],$_POST['RoleID']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Added " . $stmt->affected_rows . " row to Heroes.";
}
?>
确实是所有标识符所具有的,那么您甚至可以将其移动到(抽象)基类。
答案 1 :(得分:1)
我认为这样做的一个好方法是制作一个抽象类。通过使用它,它将为每个候选对象提供通用框架,并且还暗示特定方法需要在扩展类中。这也适用于构造函数,这是制作通用大纲的好方法
public abstract class Candidate{
//Use Vars Here
private String name;
//Constructor for the abstract class
public Candidate(String n){
//Add Normal Constructor Code Here
name = n;
}
//A possible abstract method that may vary based on the type of candidate
public abstract String getType(); //Abstract Methods must be defined in a child class
}
public class SpecificCandidate extends Candidate{
//Add other needed vars
//Normal Constructor
public SpecificCandidate(String n){
super(n); //This gets passed into the abstract constructor
}
//Define the Abstract Method
@Override
public String getName(){
return "Specific";
}
}
答案 2 :(得分:0)
根据我可以做的最好的事情是扩展Class并创建子类。
然后您可以做的是将必填字段添加到该类。
所以
public class YourCandidate extends Candidate{
// extra fields
}
为Candidate和其他GreekCandidates之间的关系映射创建一个单独的表。
采用这个: