我会想办法让服务器 - > DB关系的一种方式链接。服务器知道数据库,但不是相反。从您的评论中可以看出,Database有Logfile,而Logfile又需要一些关于Server的信息,这就是为什么引用服务器“泄漏”到DB中的原因。 将日志文件需要的信息提取到单独的实体X中,并在实例化时将其传递到数据库。注意你如何区分顾虑 - 服务器有X,DB有LogFile,它知道X,但DB对服务器本身一无所知。
private interface Server{
public String getFriendlyName();
public String getMachineNameName();
public Set<Database> getDatabases();
public void createDatabase(String name);
}
private class ProdServer implements Server{
private StuffThatLogfileNeeds stuffThatLogfileNeeds;
private HashSet<Database> dbs = new HashSet<Database>();
private ProdServer() {
stuffThatLogfileNeeds = new StuffThatLogfileNeeds("bla bla");
}
@Override
public String getFriendlyName() {
throw new RuntimeException("Not implemented yet");
}
@Override
public String getMachineNameName() {
throw new RuntimeException("Not implemented yet");
}
@Override
public Set<Database> getDatabases() {
return new HashSet<Database>(dbs);
}
@Override
public void createDatabase(String name) {
Database database = new Database(name, stuffThatLogfileNeeds);
dbs.add(database);
}
}
private class Database{
private String name;
private StuffThatLogfileNeeds stuffThatLogfileNeeds;
public Database(String name, StuffThatLogfileNeeds stuffThatLogfileNeeds) {
this.name = name;
this.stuffThatLogfileNeeds = stuffThatLogfileNeeds;
}
public String getName(){
throw new RuntimeException("Not implemented yet");
}
}
private class StuffThatLogfileNeeds{
private String foo;
private StuffThatLogfileNeeds(String foo) {
this.foo = foo;
}
public String getFoo() {
return foo;
}
}
这个问题似乎是题外话,因为它应该属于http://codereview.stackexchange.com/ –
为什么会Database类需要了解服务器的东西吗? – Bart
导致每个数据库都有(后台)关联的日志文件(Database <> ----> Logfile),并且在这个日志中需要服务器信息。这是错的吗? –