Skip to main content

Web services programming tips and tricks: Unidirectional object relationships

Implementing object relationships with a singular multiplicity

Scott W. Ambler (scott_ambler@ca.ibm.com), Practice Leader, Agile Development, Rational Methods Group, IBM
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.

Summary:  Implementing object relationships where one of the two multiplicities is singular (either one-to-one or one-to-many relationships) is simple once you understand the fundamentals.

Date:  02 Mar 2001
Level:  Introductory
Activity:  292 views
Comments:  

Relationships -- when I use the term relationship, I mean the Unified Modeling Language (UML) concepts of association, aggregration, and composition -- are implemented via the combination of attributes and methods (operations). The attributes describe the relationship, and the methods define and update the relationship. In An overview of object relationships, I discussed the fundamentals of object relationships. Now I want to begin exploring how to implement them by starting with the easiest type of relationship: A unidirectional relationship with a multiplicity of one.

A unidirectional association is one that may be traversed in one direction only. For example, in Figure 1 you see a unidirectional association from Person to Address. You know it is unidirectional because the association line has an arrowhead pointing from Person to Address. The implication is that Person objects know about their Address objects, but Address objects don't know about the Person objects that live there. In the diagram, you see that Person has scaffolding code to maintain the association, in this case the homeAddress attribute and its corresponding getter and setter method (the code for which is presented in Listing 1).


Figure 1. The Person and Address UML class model

The source code

One-to-one associations are always implemented as the combination of a simple attribute, such as an instance of an instance of Address in the case of the Person class, and a getter and setter method to manipulate that attribute.


Listing 1. Java source code to maintain the relationship between Person and Address
		
/**
 * Gets 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;
 }


Implementing business rules

It is quite common that business rules pertain to the relationships between objects. For example, the business rule "A bank customer can have no more than five bank accounts if they do not also have a brokerage account with our firm" pertains to the relationship between customers and accounts. Getter and setter methods are ideal places to implement this sort of logic, as well as applicable transformation logic pertaining to your data attributes. For example, Listing 2 presents an alternative implementation of the setter method of Listing 1. See how it first verifies the address object that it has been passed is valid, by invoking its validate() method and, if it is, then it sets the value of the homeAddress attribute. If the address is not valid, then it throws an exception.


Listing 2. Invoking a business rule

/**
 *  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();
    }
 }


Bidirectional relationships

Had the association been bidirectional, the Address class would have required the addition of a Person attribute, with corresponding getter and setter operations. Bidirectional associations are naturally harder to manage than unidirectional associations because they can be traversed in both directions. Here's a little secret: Maintaining a bidirectional association is just like maintaining two unidirectional associations. The only added complication is that you need to maintain them both in tandem. Implementing relationships with a many multiplicity, one-to-many, and many-to-many relationships, is the topic of the next tip in this series.


Resources

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.

Comments



Trademarks

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and Web services
ArticleID=11493
ArticleTitle=Web services programming tips and tricks: Unidirectional object relationships
publish-date=03022001
author1-email=scott_ambler@ca.ibm.com
author1-email-cc=