Java Reference
In-Depth Information
}
}
package user;
import account.AccountHolder;
public class UserDetails extends AccountHolder {
public synchronized
double getUserBalance(String accountNumber) {
// Use a method of AccountHolder to get the account balance
return getBalance(accountNumber);
}
}
public class User {
public boolean exists(String username) {
// Check whether user exists
return true; // Exists
}
}
Compliant Solution
The tight coupling between the classes in the two packages can be weakened by introdu-
cing an interface called
BankApplication
in a third package,
bank
. The cyclic package
dependency is eliminated by ensuring that the
AccountHolder
does not depend on
User
,
but instead relies on the interface by importing the
bank
package (and not by implement-
ing the interface).
In this compliant solution, such functionality is achieved by adding a parameter of the
interface type
BankApplication
to the
depositFunds()
method. This solution gives the
AccountHolder
a solid contract to bank on. Additionally,
UserDetails
implements the
interface and provides concrete implementations of the methods while at the same time
inheriting the other methods from
AccountHolder
.
package bank;
public interface BankApplication {
void depositFunds(BankApplication ba, String username,
double amount);
double getBalance(String accountNumber);
double getUserBalance(String accountNumber);
boolean exists(String username);
}
package account;