IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Translate into non comp-sci jargon?
-drl
New Singleton is a pattern
Consider where you think you have an object that represents a single thing. Like the keyboard. You have two options - you can make all the methods to deal with the keyboard class methods. After all there is only one and what sense does it make to have programmers allocating Keyboards willy nilly? So you make all the methods class methods and the class object itself represents THE keyboard.

This sounds great - until USB arrives and you want to write a two player game letting each player have his own keyboard. Now you've got potentially several keyboards and the keyboard class you wrote is junk - you can't use it.

So we hedge our bets - we actually create a keyboard class but put all the behavior on the instance side. Then we make the class responsible for determining how many keyboards there can be and what the allocation policy is. For conventional single keyboard apps you implement singleton. There is exactly one instance of the Keyboard and you ask the class for it.

In Java this looks like this:

public class Keyboard
{
// make the constructor private - only the class can call it
private Keyboard() { ... }

// keep a copy of the singleton
static _instance;

// provide a way to get the singleton - construct only if asked for
public static instance()
{
if(null == _instance) _instance = new Keyboard();
return _instance;
}
...
}

When you get to your game, you only have to tweak the class a little like:

private Keyboard
{
Keyboard _player1();
Keyboard _player2();

public static Keyboard player1()
{
if(null == _player1) blah blah blah

}

you get the idea. You use the class to control how many instances of a class exist.

The problem I alluded to is that - with the class instance variable being the only reference to the object, and the object being the only reference to the class, the GC concluded nobody else would care if the thing went away. This is basically true - but one win to singletons is you initialize them once and they sit there all warmed up and ready to use. Collecting the class caused a lot of thrashing of initialization code.

Stupid Java people.



Java is a joke, only it's not funny.

     --Alan Lovejoy
Expand Edited by tuberculosis Aug. 21, 2007, 06:12:42 AM EDT
New Translation
Sometimes you want an expensive to compute thing to be available in many places. A standard way to do it is to create a class that will only produce one object. If you call the class' constructor, it will produce and save the object only if it doesn't exist, then return it. On the second call it just returns it. This is called a singleton because there is a single instance of the class.

The problem is that when one person has used the singleton you have an object that exists which isn't referenced outside the class. A good GC algorithm would detect that the object is still in use and shouldn't be destroyed. In real software, the object can get detected as garbage and destroyed, forcing you to create it again. Not desired since not having to create it again and again was, after all, the point of creating singleton.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
     Java skills gap - (bluke) - (59)
         Re: Java skills gap - (deSitter) - (10)
             Believe me I know - (bluke) - (9)
                 Re: Believe me I know - (deSitter) - (8)
                     Makes sense, so it won't happen. -NT - (mmoffitt)
                     Not so easy to find a savvy person either - (bluke) - (6)
                         Re: Not so easy to find a savvy person either - (deSitter) - (5)
                             Because HR screens resumes - (bluke) - (4)
                                 Definitely agree on that last point. - (admin) - (3)
                                     The best thing I ever did was learn Smalltalk - (bluke) - (2)
                                         I dont know, but as long as they bathe regular I dont mind - (boxley)
                                         Great interview question: - (admin)
         I am afraid there is even more - (Arkadiy) - (45)
             This is why I am not a big fan of IDE's - (bluke) - (44)
                 Disagree slightly. - (mmoffitt) - (43)
                     GUI IDEs just get in my way - (admin) - (1)
                         I hate doing UI's. - (mmoffitt)
                     Intelij is a perfect example - (bluke) - (40)
                         Funny, I've found Java itself "awful" :-) -NT - (mmoffitt) - (39)
                             It is, but IntelliJ makes it less miserable -NT - (tuberculosis)
                             It all depends on where you are coming from - (bluke) - (37)
                                 Re: It all depends on where you are coming from - (deSitter) - (36)
                                     Two letters: GC - (Arkadiy) - (35)
                                         So? - (deSitter) - (30)
                                             GC is totally worth it and can be *more* efficient - (tuberculosis) - (29)
                                                 Ok I'll accept that - (deSitter)
                                                 I like ObjectiveC's memory management. -NT - (admin) - (2)
                                                     Its sort of NGC - (tuberculosis) - (1)
                                                         Kinda tedious, but not really. - (admin)
                                                 Reigning in the beast - (jb4) - (24)
                                                     Whoa - (deSitter) - (6)
                                                         "Partially persistant" things happen all of the time - (ben_tilly) - (5)
                                                             I am confused totally - (boxley) - (4)
                                                                 Its the heap - (tuberculosis)
                                                                 You can't use the stack for a lot of things - (ben_tilly) - (2)
                                                                     Re: You can't use the stack for a lot of things - (deSitter) - (1)
                                                                         A good design is a good design... -NT - (ben_tilly)
                                                     Ah, I see... - (admin) - (3)
                                                         OK, OK,...Fair enough - (jb4) - (2)
                                                             The world is full of less-than-adequate programmers - (tuberculosis) - (1)
                                                                 And plain ol' automation is the mechanism - (FuManChu)
                                                     Don't get it. - (Arkadiy) - (7)
                                                         Circular references and Class GC - (tuberculosis) - (6)
                                                             Still don't get it... - (Arkadiy) - (5)
                                                                 I guess now I do - (Arkadiy) - (4)
                                                                     Think singleton - (tuberculosis) - (3)
                                                                         Translate into non comp-sci jargon? -NT - (deSitter) - (2)
                                                                             Singleton is a pattern - (tuberculosis)
                                                                             Translation - (ben_tilly)
                                                     This was a problem in Java 1.02 as well - (tuberculosis) - (4)
                                                         Re: This was a problem in Java 1.02 as well - (dshellman) - (3)
                                                             Could you elaborate on classloader problem? - (Arkadiy) - (2)
                                                                 Re: Could you elaborate on classloader problem? - (dshellman) - (1)
                                                                     As the article says, - (Arkadiy)
                                         what does Guitar Center have to do with this? :) -NT - (Steve Lowe) - (2)
                                             Re: what does Guitar Center have to do with this? :) - (deSitter) - (1)
                                                 Yes, and in the hands of a Master - (jb4)
                                         Java has gonorrhea? Or Java == gonorrhea? I'd buy either. -NT - (mmoffitt)
         I question the source - (ben_tilly) - (1)
             Agree - marketing angle is obvious - (tuberculosis)

Some have good ideas and some.. just have ideas.
140 ms