Results 1 to 5 of 5

Thread: 1 to 1 Objects

  1. #1
    this.that is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default 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 12:02 AM. Reason: you = your ... my least favorate typo

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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
    the load function
    Could you define the function of the load methods for each of the classes?

  3. #3
    this.that is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  4. #4
    this.that is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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-07-2008 at 10:13 PM.

Similar Threads

  1. how many objects ?
    By kevinsong in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2008, 05:59 PM
  2. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 08:05 AM
  3. JSP implicit objects
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:12 AM
  4. Getting objects of a class
    By ravian in forum New To Java
    Replies: 1
    Last Post: 12-04-2007, 12:23 PM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •