• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there any way to implement a 'global' method call?  RSS feed

 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My application has got a form called 'myForm' with a text box called 'myMessages' that is used to display status and error messages. In my form code I have a method called 'myForm.writeMessage("...blah blah...")'. I want to call this from any part of my application. The trouble is my application has many objects and is deeply nested: object A creates object B which creates object C .... which creates object K.

Do I really need to pass the 'myForm' reference to all objects that I create? This is very inconvenient. I tried redesigning 'myForm.writeMessage()' as a static method but then obviously it cant refer to the form controls.

I guess I'm stuck with passing the form reference around all over the place seeing as so many parts of my application need to write messages to the form. It would be great if some clever person knew how I could achieve a static way to do this.

Many thanks in advance for lending me your awesome brain :-)

Tom
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are in fact valid reasons for wanting to call this method from "anywhere" (which seems a bit dubious), then you could use a Singleton. Google will have plenty of examples available, as well as plenty pointing out how grossly overused Singleton is.
 
Tommy Mato
Greenhorn
Posts: 26
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, Thanks for your reply. I will look up Singleton. I've heard of this design pattern. I'm guessing it is a way of obtaining an object instance from a static method? I did consider that but not sure if this is the way to go - I really wanted assurance that there isn't a simpler more obvious way to achieve this.

Also - my form is essentially a GUI version of the operating system console. Just like you would use println() to display various error and status messages on the console, my myForm.writeMessage() is effectively my GUI version of println. I don't really want to pass all messages up the chain of objects created just so they can be written out to the form. That's why I want to be able to call this code from everywhere.

Once again, sincere thanks for your reply. If you have any other comments on my clarifications above then I would be grateful for them. Tom
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tommy Mato wrote:Jeff, Thanks for your reply. I will look up Singleton. I've heard of this design pattern. I'm guessing it is a way of obtaining an object instance from a static method? I did consider that but not sure if this is the way to go - I really wanted assurance that there isn't a simpler more obvious way to achieve this.


Singleton is about as simple and obvious as it gets. The only way to make it simpler is if every method in the class is static. If you're implementing an interface or extending another class whose instance methods you need, then this won't work, and you don't have an object, just a collection of related methods.

Also - my form is essentially a GUI version of the operating system console. Just like you would use println() to display various error and status messages on the console,


Actually, println() has nothing to do with the console. It's just a method of the PrintStream class. What I use to write to the console it System.out, and sometimes, System.err; these are where the association to the console is. If you want to follow that model, you could have a MySystem class or somesuch, and then have a public static final Form form or whatever. This isn't the same as a Singleton because you'd be able to create as many Form objects as you want, if some other part of the program needs one of them for some other purpose. From the perspective of the users of that object though, it's basically the same thing--using a static identifier of a known class to get to a particular instance.

I don't know enough about your overall requirements, design, or use cases to comment on whether this is a good approach, but it's one way to get what you're asking for.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!