Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-2008, 02:00 AM
Member
 
Join Date: Jul 2008
Posts: 3
this.that is on a distinguished road
1 to 1 Objects
I am totally at a loss here, and it seems so simple... I even want to say that I have run into this problem before, but cannot remember how to resolve it or if I ever did.

Here is some code:


class User {
Program program;

void load() {
program = new Program();
program.load();
}
}

class Program {
User poc;

void load() {
poc= new User();
poc.load();
}
}



Most of you probably see right off the bat what is going to happen when I say
new Program().load() ... StackOverFlow. Of course this is a simplified representation of my problem and it took me a while to find it.

The point here is that Users are assigned programs and programs are given points of contacts.

the intent of the load function is to load the object from some external source (eg a database). I wanted the object to be "complete" after the load function was called so I cannot create shallow instances of the internal objects. But that might be the only solution. I wanted to try to avoid having to load() 100 Users every time a Program is sent to output.

I do not know if this of any use, but it at least adds a little more meaning to the design: The load function corresponds with the save function which works fine but then the save is not deep.

I am wandering if there is something simple I am missing that will make me feel silly.

Thanks for your input.

Last edited by this.that : 08-06-2008 at 02:02 AM. Reason: you = your ... my least favorate typo
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2008, 02:19 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Not sure what your reason is to have one call the other and have it call the first.
To prevent too many calls, have a static counter and stop calling load() after some limit.
You have 2 load methods but only describe one
Quote:
the load function
Could you define the function of the load methods for each of the classes?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-07-2008, 09:46 PM
Member
 
Join Date: Jul 2008
Posts: 3
this.that is on a distinguished road
There isn't a concrete definition of load(). It's abstract. The idea is that the load and save functions can be defined against any back end you want to plug them up to. Be it a database, file system, XML Documents, whatever. the easiest example would be a database. So User::load() would go to database get the user's name id.. etc, then run and grab all of the programs associated with this user and load() each program. The program's load function would do the same thing in its own context. Since program "has a" user it creates a user and then load()'s that user. Which is where the problem is.

I am having a very hard time seeing the problem from the high/design level. From the design perspective I see no reason why a User cannot program.load() the program that the user is assigned to. At the same time i see no reason why a program cannot poc.load() it's point of contact. But as soon as I create a simple implementation the problem is clearly exposed.

I am not convinced that the design is flawed. If I were to to make each load() function independent, It would fix this issue but would not be a very OO solution. To force User to know how to load a Program seems to violate encapsulation. Which is why the design is the way it is. But I also see no way to "correctly" implement the load() method in a way that will not cause recursion.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-07-2008, 10:55 PM
Member
 
Join Date: Jul 2008
Posts: 3
this.that is on a distinguished road
The more I think about this the more I see it as a lost cause. My main slip up is that I failed to recognize load() as a constructor. But it is in principle:

class User {
Program assignment = new Program();
}
class Program {
User poc = new User();
}

can simply never work. Even if the real world allows it.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-08-2008, 12:09 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
I don't see how a good OOP design would have this problem. Both the classes in your example can't require a new version of the other.
Perhaps you need a source for the objects that you call to get the next user for this program or the next program for this user. The source would be a singleton containing a list. You'd call that class's getNext() method like an Iterator.
I'm assuming that each program can have one or more users and each user is using one or more programs.

Last edited by Norm : 08-08-2008 at 12:13 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how many objects ? kevinsong Advanced Java 16 07-16-2008 07:59 PM
Objects and Classes Aleve New To Java 8 12-31-2007 10:05 AM
JSP implicit objects Java Tip Java Tips 0 12-26-2007 12:12 PM
Getting objects of a class ravian New To Java 1 12-04-2007 02:23 PM
Help with Objects! Shorinhio New To Java 1 07-10-2007 11:32 PM


All times are GMT +3. The time now is 12:31 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org