Nice TWiki > Doc > DesignPatterns > SingletonPattern (r1.2) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
In Java, creating a singleton class involves StaticMethods? and StaticFields?:
package a;

public class Single
  public static Single getInstance()
    return instance;

  private static final Single instance = new Single();

You can do the following in Nice:
package a;

public class Single { ... }

let Single instance = new Single();

-- DanielBonniot

It seems to me, that you have missed very important part of Java Singleton pattern: you must define a private constructor for Single() to disallow creation of another instances of Single through call to default constructor. This raises the question about Nice's ability to implement a Singleton in general: if you don't have a constructors, one can write anywhere

let Single anotherInstance = new Single();
and get as many instances of Singleton, as one wants... Do you have any mechanism to prevent this?

-- TWikiGuest

You are right. Here is my proposal: a class could be declared private, which would mean that the constructor can only be called from the same file that declares the class. In Java a class cannot be private. This would be equivalent to a private constructor in Java. In particular, it would still be possible to use the class name in a type outside of the declaring file. -- DanielBonniot - 23 Jul 2002

Topic SingletonPattern . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.2 - 30 May 2003 - 11:19 GMT - DanielBonniot
Parents: WebHome > DesignPatterns
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.