javablogspot

Just another WordPress.com weblog

Java Term of the Week: Dependence

Posted by damuchinni on March 8, 2009

After looking at aggregation, composition and association it’s time to finish off the definitions of class relationships with dependence. Dependence is a simple relationship between two classes where one class uses the other. It’s described as the “uses-a” relationship. Typically the relationship between the two classes is short-term. An object is passed as a parameter in a method call and once the method has finished executing the object is discarded.

For example, 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 the Account class but once the method has finished executing it will be discarded.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: