We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Singleton getInstance() for PApplet
Page Index Toggle Pages: 1
Singleton getInstance() for PApplet ? (Read 1258 times)
Singleton getInstance() for PApplet ?
Dec 30th, 2008, 6:11pm
 
I am not an experienced java programmer, and relatively new to Processing, but I want to use Processing in Eclipse, as I like having a debugger and autoComplete.   For more complex applications with many classes I find the native IDE to be a little limiting (while it is GREAT for quick sketches.)  The tutorial on integrating processing with eclipse demonstrates passing a reference of PApplet into all class constructors, which seems like a bit of a hack.

Would it be possible to extend PApplet with a static getInstance() method that follows the singleton pattern?  It would seem then that if a class needs access to PApplet it could grab it via getInstance() and you wouldn't have to adulterate all of your constructor interfaces.  

I could not find any other threads in the forums on this, if anyone has done this already please let me know.
Re: Singleton getInstance() for PApplet ?
Reply #1 - Dec 31st, 2008, 1:28pm
 
A technique I saw in flux.vertext library is to make all classes extend an abstract class with a protected static PApplet field.
A static method to register PApplet is called in the main() function, then all classes can use the PApplet field directly to call Processing functions.
This gets a bit in the way of inheritance, but it is rarely a major issue (modern OOP practices advise to use composition over inheritance, except perhaps for libraries and frameworks).
Re: Singleton getInstance() for PApplet ?
Reply #2 - May 8th, 2009, 3:38pm
 
Hi I was thinking of doing the same thing. I did the following in my main application class:

Code:
	

private static PApplet p5;

public static PApplet getInstance(){

return p5;

}

public void setup() {

p5 = this;
       
}


In other classes you just need import your main app class and call the static getInstance() method.

Seems to work fine so far, would be good to know if anyone knows wether or not this approach might cause me problems later down the line.

Page Index Toggle Pages: 1