Java Reference
In-Depth Information
Lines 114 through 117 in Figure 9-24 on the previous page create the
accessor method for the remainingUses attribute, which is an example of an
instance variable with read-only access. With the current design, once the value
for remaining uses of a password is set by the constructor, it cannot be changed.
If such a feature were desired later, it easily could be added to the Password class
without affecting current users of the class. This illustrates the iterative nature of
object-oriented programming described earlier in this chapter.
Some attributes, or properties, of an object may not appear as instance vari-
ables in the class definition. These attributes, or properties, may be properties of
a parent class which the object has inherited; in this case, the parent class should
provide accessor and/or mutator methods to provide access, as appropriate. For
example, a user program may need to know the size of the pswdHistory list, but
that attribute is not specifically declared in the Password class. The pswdHistory
object inherits certain attributes from ArrayList, such as size. Access is needed,
therefore, to the properties of another object, which is itself an instance variable
of the class. Such is the case with the ArrayList object in the Password class. Lines
119 through 122 code the accessor method, getHistorysize(), which needs to get
the password history size to return the number of elements stored in the history.
The password history size is a property of the ArrayList object referenced by the
instance variable, pswdHistory, and is not a property of the Password class.
Objects of type ArrayList have an accessor method, size(), which returns the
desired value for history size. The getHistorySize() method returns the value
obtained from calling the size() method of the ArrayList, pswdHistory. In doing
so, the getHistorySize() method provides a type of read-only value, even though
it does not return the value of an instance variable of Password.
Occasionally, users will need to know some aspect of an object which is not
modeled by a single attribute, but by some condition, calculation, or combina-
tion of attribute values. Such is the case with the requirement to notify users if a
password is expiring. This condition does not have a direct attribute to indicate
the password is expiring. Note that this condition is different from a password
that has expired, for which there is an attribute. Rather, it is a combination of
two conditions: (1) if the password is set to expire automatically, and (2) if the
number of remaining uses is at or below the threshold, or limit, for notifying
users. Figure 9-25 displays the code for the isExpiring() read-only accessor
method for the Password class. Line 126 sets the value of the boolean variable,
expiring, to false. Line 128 then checks to see if the password is set to expire
automatically and if the number of remaining uses is at or below the threshold,
or limit, for notifying users. If both conditions are true, line 129 changes the
value of expiring to true and line 131 returns the value.
Even though the isExpiring() method does not return the value of a given
instance variable, you can write a method that provides information on a combi-
nation of attributes — such as, if the current password is at the point where it
will soon expire. In the Password class, the isExpiring() method will return a
boolean value named, expiring. As such, it can be viewed as a type of pseudo-
accessor method that provides a read-only value.
Search WWH ::




Custom Search