Skip to main content

Web services programming tips and tricks: An overview of object relationships

The basics of UML and Java associations

Scott W. Ambler, Practice Leader, Agile Development, Rational Methods Group, IBM, Software Group
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:  Objects have associations to, or relationships with, other objects. Understanding the nuances of relationships in object modeling is the first step to understanding how to implement them in your Java source code.

Date:  08 Feb 2001
Level:  Introductory
Activity:  968 views

In the real world, objects have relationships -- associations to other objects. The relationships between objects are important because they help us to define how objects interact with each other. For example, students take courses, professors teach courses, criminals rob banks, politicians kiss babies, and captains command starships. Take, teach, rob, kiss, and command are all verbs that define associations between objects. You want to identify and document these relationships, so that you can gain a better understanding as to how objects interact with one another.

Types of relationships

The Unified Modeling Language (UML) distinguishes between three different types of relationships between objects: association, aggregation, and composition. An association is a simple relationship between two objects without any additional implications. Aggregation is a narrower type of association that represents "is part of" relationships between two objects, such as the fact that an item is part of an order. Composition is a strong form of aggregation where the "whole" is completely responsible for its parts and each "part" object is only associated to the one "whole" object, for example an airplane is composed of wings and a fuselage.


Describing relationships

Not only must you identify the relationship(s) between classes, you must also describe the relationship. For example, it's not enough to know that students take seminars. How many seminars do students take? None, one, or several? Furthermore, relationships are two-way streets: not only do students take seminars, but seminars are taken by students. This leads to questions like: How many students can be enrolled in any given seminar, and is it possible to have a seminar with no one in it? The implication is that you also need to identify the cardinality and optionality of an association. Cardinality represents the concept of "how many," whereas optionality represents the concept of "whether you must have something."

It is important to note is that the UML chooses to combine the concepts of optionality and cardinality into the single concept of multiplicity. However, my experience is that significant value exists in considering the two concepts in isolation: For each direction of an association there is value in asking whether the association must exist (optionality) and how many could possibly exist (cardinality)? For example, consider the "teaches" association between professors and seminars. I would ask:

  • Must a professor teach a seminar? Or is it possible that some professors don't do any teaching?
  • How many seminars could a single professor teach?
  • Must a seminar be taught by a professor? Or is it possible that someone who isn't a professor could teach a course?
  • How many professors are potentially needed to teach a single seminar?

Modeling relationships

When you model relationships in UML class diagrams, you show them as a thin line connecting two classes, as you see in Figure 1. Relationships can become quite complex; consequently, you can depict two things about them on your diagrams. Figure 1 shows the common items to model for a relationship. You may want to refer to The Unified Modeling Language Reference Manual (see Resources) for a detailed discussion, including the role and cardinality on each end of the relationship, as well as a label for the relationship. The label, which is optional, is typically one or two words describing the relationship. The multiplicity of the relationship is labeled on either end of the line, one multiplicity indicator for each direction (Table 1 summarizes the potential multiplicity indicators you can use). At each end of the relationship, the role -- the context that an object takes within the relationship -- may also be indicated.

Table 1. UML multiplicity indicators

IndicatorMeaning
0..1Zero or one
1One only
0..*Zero or more
1..*One or more
nOnly n (where n > 1)
0..nZero to n (where n > 1)
1..nOne to n (where n > 1)

As you can see in Figure 1, aggregation is indicated using a hollow diamond and composition is indicated with a darkened diamond, both of which are at the end of the association line nearest the "whole" object.


Figure 1. Notation for modeling relationships on UML class diagrams
Notation for modeling relationships on UML class diagrams

Introduction to implementing relationships

Relationships are maintained through the combination of attributes and methods. The attributes store the information necessary to maintain the relationship and methods keep the attributes current. For example, the Student class of Figure 2 would potentially have an attribute called takes. It may have an array, which is used to keep track of the Seminar objects the student is currently taking. The Student class might also have methods such as addSeminar and removeSeminar to add and remove seminar objects into the array. The Seminar class would have a corresponding attribute called students and methods called addStudent and removeStudent to maintain the relationship in the opposite direction. All of this has a significant implication: because attributes and methods are inherited, associations are too.

In Figure 2, the unidirectional association "holds" between Employee and Position would be easier to implement because you only need to traverse it in one direction: from Employee to Position. Therefore, Employee would have an attribute called position and methods, called something like setPosition and getPosition to maintain the association. There would be nothing added to Position because there is no need for position objects to collaborate with employee objects, therefore, there is no added code to maintain the association in that direction.


Figure 2. Examples of simple relationships
Examples of simple relationships

When you are modeling, the common style is to assume the attributes and methods exist to maintain associations (you don't need to show them on your diagrams). You should consider setting naming conventions for these attributes and methods. I typically use the role name or the class name for the attribute name and method names, such as addAttributeName and removeAttributeName for many associations, and setAttributeName and getAttributeName for single associations when I'm writing the source code, but this will be the topic of future articles.


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 (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

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=11484
ArticleTitle=Web services programming tips and tricks: An overview of object relationships
publish-date=02082001
author1-email=scott_ambler@ca.ibm.com
author1-email-cc=

My developerWorks community

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.

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).

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).

Rate a product. Write a review.

Special offers