| | |
Design flaw
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
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....
// the Listener object...
Hope this all make sence?
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....
Java Syntax (Toggle Plain Text)
variables.... ... private static Logger diagnosticLog = DiagnosticLogger.getInstance().getLogger(); .. .. public static Gui getInstance() { diagnosticLog.log(Level.INFO, "HELLlllllllllllOOOOOOOOOOOOOOOO"); if (frame == null) { frame = new Gui("BTServer"); frame.setSize(60,40); createAndShowGUI(); } return frame; } //Constructor of the class.... private Gui(String name) { super(name); Listener listenerOnMemoryArea = new Listener(); SharedMemorySection.getInstance().registerListenerOntoSharedMemory (listenerOnMemoryArea); diagnosticLog.log(Level.INFO, "Creeating an object of GUI()");
Java Syntax (Toggle Plain Text)
Gui instance; public Listener() { instance = Gui.getInstance(); } /** * Puts the strings onto a canvas * * @param stringList - the list of messages to be displayed to the user. */ public void printMessagesToUser ( List<String> stringList) { //Iterate through the list Iterator <String> it = stringList.listIterator (); while(it.hasNext ()) { String str = StringFormatter.stringFormatter (it.next ()); if(str == null) { return; } // instance.logThisMessage ("USER",str); System.out.println(" ******************" + this.toString() + " " + str); } } }
Hope this all make sence?
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.
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.
![]() |
Similar Threads
- Presario 700 screen will not stay open (Monitors, Displays and Video Cards)
- Files Missing with Maxtor One Touch II USB 200GB External HDD (Storage)
- Dell Inspiron 1150 (Motherboards, CPUs and RAM)
- converting base class to dervived class (C)
- Is there a 'parent' stl container class? (C++)
- Playing cards (C++)
- Presario 700- won't do anything! (Troubleshooting Dead Machines)
- Netgear router issue...read this if you have one (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: ping is not successful.
- Next Thread: Help me for main project....
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






