Design flaw

Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Design flaw

 
0
  #1
Jan 17th, 2008
Hey guys, gonna try and explain this in an hopefully understandable way.

I've got my GUI which is called a GUI class which is a singleton so I can access it anywhere in my code and manipulate the GUI object.

I've also got a sharedMemorySection which again is a singleton which stores an ArrayList<string> allowing the system to post messages into the arraylist.

The problem starts to come to light when the messageListeners are invoked since they grab an instance of the GUI class in order to post a message but in order to create a GUI instance we need a messageListener and therefore I seem to get into an inifintive loop and a runtimestack overflow occurs.

Basically
Gui -> makes listener (but GUI is still set to NULL because it ain't finished been set up)
listener -> needs object of GUI so requests a new object since its still set to NULL

Hence a infinitive loop of getInstance calls occurs.

I'm wondering if theres a way rnd this?

//my customer logger that enables me to save messages to the arrayList

//in GUI class....
  1.  
  2. variables....
  3. ...
  4. private static Logger diagnosticLog = DiagnosticLogger.getInstance().getLogger();
  5. ..
  6. ..
  7. public static Gui getInstance() {
  8.  
  9. diagnosticLog.log(Level.INFO, "HELLlllllllllllOOOOOOOOOOOOOOOO");
  10. if (frame == null) {
  11. frame = new Gui("BTServer");
  12.  
  13. frame.setSize(60,40);
  14. createAndShowGUI();
  15. }
  16. return frame;
  17. }
  18. //Constructor of the class....
  19. private Gui(String name) {
  20. super(name);
  21.  
  22. Listener listenerOnMemoryArea = new Listener();
  23.  
  24. SharedMemorySection.getInstance().registerListenerOntoSharedMemory (listenerOnMemoryArea);
  25.  
  26. diagnosticLog.log(Level.INFO, "Creeating an object of GUI()");
// the Listener object...
  1. Gui instance;
  2.  
  3. public Listener() {
  4. instance = Gui.getInstance();
  5. }
  6.  
  7.  
  8.  
  9. /**
  10.   * Puts the strings onto a canvas
  11.   *
  12.   * @param stringList - the list of messages to be displayed to the user.
  13.   */
  14. public void printMessagesToUser ( List<String> stringList) {
  15.  
  16. //Iterate through the list
  17. Iterator <String> it = stringList.listIterator ();
  18. while(it.hasNext ()) {
  19. String str = StringFormatter.stringFormatter (it.next ());
  20. if(str == null) {
  21. return;
  22. }
  23. // instance.logThisMessage ("USER",str);
  24. System.out.println(" ******************" + this.toString() + " " + str);
  25. }
  26.  
  27. }
  28. }

Hope this all make sence?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Design flaw

 
1
  #2
Jan 17th, 2008
Well, which one has no context without the other? Does a listener have any useful function without a GUI? What about the GUI? It seems to me the GUI can exist without a listener, but it just won't do much until it has one. So you could create the GUI and then create the listener passing a reference to the GUI.

On the other hand, if your listener is really a dispatcher, which it kind of appears to be, it can exist and function without anything to pass message along to - it just ends up trying to send a message to an empty list until receivers register.

Consider also that maybe you don't really need those classes to be explicit singletons. They may only ever have one instance in the context of your app, but that doesn't mean it has to be enforced by the class design. Singletons can cause a lot of headaches down the road if you aren't absolutely certain of their singularity.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC