Dependence
Definition:
Dependence is a relationship between two classes where one class uses another. It's described as the "uses-a" relationship. Typically the relationship between the two classes is short-term. In Java, the dependence relationship can be best seen when an object is passed as a parameter in a method call. Once the method has finished executing, the object is no longer needed and is discarded.
Imagine a Telephone class that allows users to make phone calls. It is a pre-pay system that only allows a user to start a phone call if they have at least five dollars in their account. The user's account details are kept in an Account class:
The Telephone class is using an Account object but once the method has finished executing it will be discarded.
Dependence is a relationship between two classes where one class uses another. It's described as the "uses-a" relationship. Typically the relationship between the two classes is short-term. In Java, the dependence relationship can be best seen when an object is passed as a parameter in a method call. Once the method has finished executing, the object is no longer needed and is discarded.
Examples:
Imagine a Telephone class that allows users to make phone calls. It is a pre-pay system that only allows a user to start a phone call if they have at least five dollars in their account. The user's account details are kept in an Account class:
public class Account { private BigDecimal balance; public BigDecimal getBalance() { return balance; } //rest of account class }public class Telephone { public void startCall(Account customerAccount) { if (customerAccount.getBalance().compareTo(new BigDecimal(5.00)) >= 0) { //start call } } //rest of Telephone class }
The Telephone class is using an Account object but once the method has finished executing it will be discarded.