Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Singleton - how to  (Read 1482 times)
0 Members and 1 Guest are viewing this topic.
Offline Preston

Senior Devvie


Medals: 4



« Posted 2003-10-20 06:20:21 »

Hi. I'd like to use Singletons. Maybe the following singleton pattern class would do this:
(Addendum: doesn't work, because of the static sInstance for any singleton inheritance)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
abstract public class Singleton
{
  private static Singleton sInstance;

  public Singleton()   {
    assert sInstance == null;
    sInstance = this;
  }

  public static Singleton getInstance() {
    assert sInstance != null;
    return sInstance;
  }
}

 
Now let's define a class which wants to be a singleton.
1  
2  
3  
4  
5  
6  
class Example extends Singleton
{
  public void method() {
    // bla
  }
}


Finally let's fetch the instance of the example class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
{
  // Once the example class is being created.
  Example mMyexample = new Example();
}

{  
  // Later on, we want to access the singleton class
  Example ex = (Example) Example.getInstanz();  // How to avoid the Cast?
  ex.method();
}


Is it possible to avoid the cast operation?
Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #1 - Posted 2003-10-20 06:49:18 »

usually the singleton interface is implemented in the specific class - and not inherited.  In that way, no cast is ever needed. If you do choose to implement the singleton pattern using inheritance, you will need to cast.

Offline Jens

Senior Devvie




Java for games!


« Reply #2 - Posted 2003-10-20 06:55:13 »

Usually a private constructor is used (no inheritance).

Xith3D Getting Started Guide (PDF,HTML,Source)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Preston

Senior Devvie


Medals: 4



« Reply #3 - Posted 2003-10-20 07:18:48 »

I see.
This (=no inheritance) has been the way I did it, but... then I thought: Do I really have to implement the above singleton methods in any class which wants to be a singleton?

Obviously I have to do so.
Thank you.
Offline tortoise

Junior Devvie




<3 Shmups


« Reply #4 - Posted 2003-10-20 15:33:57 »

The approach you were attempting is more commonly done in C++, where they have a few more tricks up their sleeves they can use to pull it off. In particular they can use templates to avoid the casting.

You can of course just use all static methods and avoid the singleton object all together. That's also nice in some cases.
Offline Preston

Senior Devvie


Medals: 4



« Reply #5 - Posted 2003-10-21 04:59:06 »

Quote
The approach you were attempting is more commonly done in C++, where they have a few more tricks up their sleeves they can use to pull it off. In particular they can use templates to avoid the casting.

Yes.

Quote
You can of course just use all static methods and avoid the singleton object all together. That's also nice in some cases.

This is the way I do it now. Any singleton class has its own few lines of code for the handling of the static sInstance. No casting. However I'd still prefer an abstract singleton base class with no casting.

In my original inheritance example there's a mistake:
1  
2  
3  
abstract public class Singleton 
{
  private static Singleton sInstance;

This won't work because any inherited class of the Singleton base class will use the same sInstance.

How would a real Singleton base class have to work in Java - with casting then?
Offline tortoise

Junior Devvie




<3 Shmups


« Reply #6 - Posted 2003-10-21 11:11:09 »

But even if static fields could be overridden, you'd still have the "problem" that there's no multiple inheritance in Java. So your singleton could never descend from anything else, and a Singleton interface wouldn't work.

I suppose you could have a SingletonFactory that knows how to create instances of certain classes. Whenever you do something like getSingleton("one.of.my.Classes"), it'd either create the instance or fish it out of a Map. But then you'd have to make all those classes in the same package as the singleton for any kind of privacy, and it's just ugly. Java can't really be bent like this.
Offline Preston

Senior Devvie


Medals: 4



« Reply #7 - Posted 2003-10-21 11:27:57 »

Thanks, I see.

Well, the manual way is OK, too. It means to have 4 little lines of code + 1 line of var declaration in each class which wants to be a singleton, that's pretty OK.

Java is good. I like it much.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!