Are circular class dependencies bad from a coding style point of view?
Example:
In a database application we have two classes, one encapsulating information about a single database (DBInfo
) and one class which can create a database connection. (ConnFactory
)
DBInfo
has a getConnection
method which uses ConnFactory
to create a connection. But ConnFactory
itself needs a DBInfo
object to do so.
Like this: (Any coding styles disregarded for the sake of readability)
class DBInfo {
String name;
String connectionUrl;
Connection getConnection() {
return ConnFactory.getConnection(this);
}
}
class ConnFactory {
Connection getConnection(DBInfo toWhat) {
return new Connection(toWhat.connectionUrl);
}
}
My co-workers argue that this is bad practice and it would be better if there were only one direction of dependencies and no circular ones like here.
Is this bad practice, an anti-pattern or a code smell? Are there any drawbacks?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…