Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

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
Also available in:   Japanese

Activity:  4886 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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

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=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.

Special offers