///Accessors Increase Robustness of Java Code

Accessors Increase Robustness of Java Code

Accessors increase robustness of Java code

Scott Ambler explains how to use accessors, and why their use is one of the most important standards your organization can enforce. This article is modified from Chapter 8 of The Object Primer 2nd Edition.

Encapsulation and information hiding are fundamental principles that lead to the design of robust classes and components. An important technique that Java developers can employ to increase encapsulation, and to support information hiding, is the appropriate use of accessor methods.

Accessors — member functions that directly manipulate the value of fields — come in two flavors: setters and getters. A setter modifies the value of a field, whereas a getter obtains its value. Although accessors add minimal overhead to your code, the loss in performance is often trivial compared to other factors (such as questionable database designs). Accessors help to hide the implementation details of your classes and, thus, increase the robustness of your code. By having, at most, two control points from which a field is accessed, one setter and one getter, you are able to increase the maintainability of your classes by minimizing the points at which changes need to be made.

Figure 1 depicts the implementation of the getter and setter member functions for the homeAddress field of the Person class. Getters and setters are useful because they enable you to encapsulate important business rules, transformation logic, and validation logic that are applicable to your data fields. For example, Figure 2 presents an alternative implementation of the setter member function. See how it first verifies that the address object it has been passed is valid by invoking its validate() member function. If it is valid, then the function sets the value of the homeAddress field. If the address is not valid, the function throws an exception.

Figure 1. The getter and setter for the homeAddress field of the Personclass




/**
* Answers the person's home address
*
* @return homeAddress
*/

public Address getHomeAddress()
{
return homeAddress;
}

/**
* Sets the person's home address
*
* @param homeAddress
*/

public void setHomeAddress(Address homeAddress)
{
this.homeAddress = homeAddress;
}



Figure 2. Alternative implementation of the setHomeAddress() member function




/**
* Sets the person's home address
*
* @param homeAddress
* @return homeAddress
*/

public Address setHomeAddress(Address homeAddress) throws InvalidDataException
{
// Only set the address if it is valid
if (homeAddress.validate() ) {
this.homeAddress = homeAddress;
}

else {
throw new InvalidDataException();
}
}

One of the most important standards your organization can enforce is the use of accessors. Some developers don’t want to use accessor member functions because they don’t want to type the few extra keystrokes required (for example, for a getter, you need to type in get and () above and beyond the name of the field). The bottom line is this: the increased maintainability and extensibility from using accessors more than justifies their use.

Getter member functions should be given the name get + field name, unless the field represents a Boolean (true or false), and then the getter is given the name is + field name. Setter member functions should be given the name set + field name, regardless of the field type. As you see in Table 1, the field name is always in mixed case with the first letter of each word capitalized. This naming convention is used consistently within the Java Development Kit (JDK) and is required for JavaBeans development and for persistent fields in Enterprise JavaBean (EJB) 2.0.

Table 1. Example accessor names

Field Type Getter name Setter name
name string getName setName
homeAddress Address object getHomeAddress setHomeAddress
persistent boolean isPersistent setPersistent
zipCode int getZipCode setZipCode
instructors Vector of Professor objects getInstructors setInstructors

Resources

  • Other tips and articles by Scott Ambler on developerWorks
  • Building Object Applications That Work: Your Step-By-Step Handbook for Developing Robust Systems with Object Technology by Scott W. Ambler. New York: Cambridge University Press, 1998.
  • Process Patterns — Building Large-Scale Systems Using Object Technology by Scott Ambler. New York: Cambridge University Press, 1998.
  • The Object Primer 2nd Edition by Scott W. Ambler. New York: Cambridge University Press, 2000.
  • The Unified Process Construction Phase by Scott W. Ambler and Larry L. Constantine. Gilroy, CA: R&D Books, 2000.
  • The Java Language Specification by James Gosling, Bill Joy, and Guy Steele. Reading, MA: Addison-Wesley Longman, Inc., 1996.
  • Advanced Java: Idioms, Pitfalls, Styles and Programming Tips by Chris Laffra. Upper Saddle River, NJ: Prentice Hall Inc., 1997.
  • Essential Java Style: Patterns for Implementation by Jeff Langr. Upper Saddle River, NJ: Prentice-Hall PTR, 1999.
  • Java 2 Performance and Idiom Guide: Guidelines for Java 2 Performance, Coding, and Testing by Craig Larman and Rhett Guthrie. Upper Saddle River, NJ: Prentice Hall Inc., 2000.
  • Object-Oriented Software Construction, Second Edition by Bertrand Meyer. Upper Saddle River, NJ: Prentice-Hall PTR, 1997.
  • The Elements of Java Style by Alan Vermeulen, Scott W. Ambler, Greg Bumgardner, Eldon Metz, Trevor Misfeldt, Jim Shur, and Patrick Thompson. New York: Cambridge University Press, 2000.
  • 2010-05-26T17:18:46+00:00 March 4th, 2003|Java|0 Comments

    About the Author:

    Scott W. Ambler is President of Ronin International, a consulting firm specializing in object-oriented software process mentoring, architectural modeling, and Enterprise JavaBeans (EJB) development. He has authored or co-authored several books about object-oriented development, including the recently released The Object Primer 2nd Edition, which covers, in detail, the subjects summarized in this article. He can be reached at scott.ambler@ronin-intl.com and at his Web site at www.ambysoft.com.

    Leave A Comment