Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Circular dependency

Reply
Thread Tools

Re: Circular dependency

 
 
John C. Bollinger
Guest
Posts: n/a
 
      08-20-2003
Brian J. Sayatovic wrote:
> 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?


You may not need to design around it, but if you want to do (I would)
then one alternative would be to make State an interface, and have
Persistence.getState(int) return an instance of a nested class that
implements State. In my opinion, a tight coupling such as the one you
have shown needs to be either loosened or coallesced, and my suggestion
does both.


John Bollinger


 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
circular dependency between jars DinkyToy Java 0 04-07-2005 02:12 PM
Semi-circular definitions (plus circular references) Kiuhnm C++ 16 01-03-2005 03:49 AM
Re: Circular dependency Andrew Cowper Java 0 08-20-2003 03:30 PM
Re: Circular dependency John Hodgson Java 0 08-20-2003 01:22 PM
Re: Circular dependency Roedy Green Java 0 08-19-2003 07:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57