Nice TWiki > Doc > DesignPatterns > SingletonPattern 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

This meaning of 'private' for classes would also enable enumerations and other algebraic datatypes:

private class Severity {}
final class DEBUG extends Severity {}
final class TRACE extends Severity {}
final class ERROR extends Severity {}

-- BrynKeller - 11 Jun 2003

Topic SingletonPattern . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.3 - 11 Jun 2003 - 20:02 GMT - BrynKeller
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.