jQuery in Action, 3rd edition
The moose likes OO, Patterns, UML and Refactoring and the fly likes Circular Reference - Any suggestions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Circular Reference - Any suggestions?" Watch "Circular Reference - Any suggestions?" New topic

Circular Reference - Any suggestions?

Clarice Doe

Joined: Dec 24, 2004
Posts: 21
I have number of Employee objects and a number of company objects. Now I want to assign some employees to one company, some other employees to some other company and so on.

In OO terms, a company has zero or more employees and an employee is associated with one and only one office. In Java code it translates to

But is this sort of circular reference (company holds employee and employe has a referece to company)acceptable? Is there a better way of putting it in code?

[ September 06, 2005: Message edited by: Clarice Doe ]
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

This is a form of tight coupling. Virtually all design tutorials advocate loose coupling. Putting yourself in the best position to reuse object types is one reason. Keeping the interface of each object functionally separate is another.

Knowing what Employees and Companies do in your application will do make shed more light. Something as simple as a hashtable, where each Employee is a key and each Company a value. Something more sophisticated such as a Composite pattern, where each Company represents its Employees by both membership and responsibilities over other Employees...it's a question of attributes you want to emphasize that I think will guide you along further.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Micheal is right. The first thing we need to think about is "what behaviour does a company object in my system? What about Employee?"

Then we can decide about wether Employee actually needs to know about its company, and if so how.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link: http://aspose.com/file-tools
subject: Circular Reference - Any suggestions?
It's not a secret anymore!