Java Reference
In-Depth Information
Eliminating cycles between packages has several advantages.
Testing and maintainability. Cyclic dependencies magnify the repercussions of
changes or patches to source code. Reducing the repercussions of changes simpli-
fies testing and improves maintainability. Inability to perform adequate testing be-
cause of cyclic dependencies is a frequent source of security vulnerabilities.
Reusability. Cyclic dependencies between packages require that the packages be
released and upgraded in lockstep. This requirement reduces reusability.
Releases and builds. Avoiding cycles also helps to steer the development toward
an environment that fosters modularization.
Deployment. Avoiding cyclic dependencies between packages reduces coupling
between packages. Reduced coupling reduces the frequency of runtime errors
such as ClassNotFoundError . This, in turn, simplifies deployment.
Noncompliant Code Example
Thisnoncompliantcodeexamplecontainspackagesnamed account and user thatconsist
of the classes AccountHolder , User , and UserDetails , respectively. The class UserDe-
tails extends from AccountHolder because a user is a kind of account holder. The class
AccountHolder depends on a non-static utility method defined in the User class. Like-
wise, UserDetails depends on AccountHolder by extending it.
Click here to view code image
package account;
import user.User;
public class AccountHolder {
private User user;
public void setUser(User newUser) {user = newUser;}
synchronized void depositFunds(String username,
double amount) {
// Use a utility method of User to check whether
// username exists
if (user.exists(username)) {
// Deposit the amount
}
}
protected double getBalance(String accountNumber) {
// Return the account balance
return 1.0;
Search WWH ::




Custom Search