Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Circular Reference - Any suggestions?

 
Clarice Doe
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks
[ September 06, 2005: Message edited by: Clarice Doe ]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic