Re: Circular dependency

Discussion in 'Java' started by John Hodgson, Aug 20, 2003.

  1. John Hodgson

    John Hodgson Guest

    (Brian J. Sayatovic) wrote in message news:<>...
    > I have what I believe is a circular dependency:
    >
    > public class State {
    > public static final State INITIAL_STATE = loadState(0);
    > protected static State loadState(int value) {
    > Persistence persistence = Persistence.getDefaultInstance();
    > return persistence.getState(0);
    > }
    > }
    >
    > public class Persistence {
    > public static Persistence getDefaultInstance() {
    > // ... *standard* singleton pattern
    > }
    > public State getState(int value) {
    > // ... SELECT * FROM STATES WHERE STATE_VALUE=?
    > }
    > }
    >
    > I believe that because the Persistence class references the State
    > class (as a return type), that it wants to load and initialize the
    > State class first. However, the State class also needs the
    > Persistence class's default instance to be set so that it can load
    > it's static members from the DB.
    >
    > The bit of code you don't see here is that I'm not really using the
    > standard singleton pattern. Instead, I have a 'main' class that
    > constructs a Persistence instance and sets it as the default instance
    > of Persistence. So when that first instance is being constructed, and
    > thus, it's class is being loaded, I'm guessing it also tries to load
    > the State class. However, the state class tries to access the default
    > instance which has not yet been set.
    >
    > I'm drawing a blank. How can I design around this?
    >
    > Regards,
    > Brian.


    Objects that have a complicated creation should probably be
    created in a two-step process. The first step is a lightweight
    construction, e.g., an empty default constructor. The second
    step is an initialization, for example, as in
    javax.servlet.GenericServlet. In that way, the objects exist
    before they have to get references to each other. Also, a
    third-party object may help, e.g., a co-ordinator, or manager,
    object for the objects in question. The co-ordinator exists
    beforehand, and creates the objects, and then provides the
    objects with references to each other.
     
    John Hodgson, Aug 20, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    Re: Circular dependency

    Roedy Green, Aug 19, 2003, in forum: Java
    Replies:
    0
    Views:
    395
    Roedy Green
    Aug 19, 2003
  2. Andrew Cowper

    Re: Circular dependency

    Andrew Cowper, Aug 20, 2003, in forum: Java
    Replies:
    0
    Views:
    395
    Andrew Cowper
    Aug 20, 2003
  3. John C. Bollinger

    Re: Circular dependency

    John C. Bollinger, Aug 20, 2003, in forum: Java
    Replies:
    0
    Views:
    400
    John C. Bollinger
    Aug 20, 2003
  4. DinkyToy

    circular dependency between jars

    DinkyToy, Apr 7, 2005, in forum: Java
    Replies:
    0
    Views:
    401
    DinkyToy
    Apr 7, 2005
  5. Kiuhnm
    Replies:
    16
    Views:
    745
    Jonathan Mcdougall
    Jan 3, 2005
Loading...

Share This Page