• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Circular Reference - Any suggestions?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic