Nice TWiki > Doc > SingletonPattern (r1.1 vs. r1.2) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
 <<O>>  Difference Topic SingletonPattern (r1.2 - 30 May 2003 - DanielBonniot)
Changed:
<
<

>
>

Changed:
<
<

>
>


Changed:
<
<

public Single getSingle() = instance;

var Single instance = new Single();

>
>

let Single instance = new Single();

Deleted:
<
<

The instance variable should be declared private, or final. This is not currently supported by the compiler. Ehancements in this area are planned.

Changed:
<
<

-- DanielBonniot

>
>

-- DanielBonniot

Changed:
<
<

var Single anotherInstance = new Single();

>
>

let Single anotherInstance = new Single();

Changed:
<
<

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

>
>

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


 <<O>>  Difference Topic SingletonPattern (r1.1 - 30 Jan 2003 - TWikiGuest)
Added:
>
>

%META:TOPICINFO{author="guest" date="1043948085" format="1.0" version="1.1"}% %META:TOPICPARENT{name="DesignPatterns"}% 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 { ... }

public Single getSingle() = instance;

var Single instance = new Single();
The instance variable should be declared private, or final. This is not currently supported by the compiler. Ehancements in this area are planned.

-- 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

var 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 . { View | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.1 - 30 Jan 2003 - 17:34 GMT - TWikiGuest
Revision r1.2 - 30 May 2003 - 11:19 GMT - DanielBonniot
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.