Implementing singletons in Dart

812 views
Skip to first unread message

Greg Lowe

unread,
Dec 12, 2013, 6:23:37 PM12/12/13
to mi...@dartlang.org
It seems that the standard style of implementing singletons in Dart is to use factory constructors.

As seen in this answer:

Personally I find the use site "new Singletion()" a bit confusing, as new is not behaving as it normally would.

Why is this style preferred over importing a top level getter?

Example of alternate style here:

Cogman

unread,
Dec 12, 2013, 8:16:41 PM12/12/13
to mi...@dartlang.org
The first style looks very Java-esq.  That is probably where it starts.  The other nice thing about the first example is that you don't have to care, as a consumer of the API, that the thing you are getting is a singleton.  Rather, it looks more like you are just making a new object.

The second one makes it a bit more apparent that the thing you are making is a singleton.

The benefit of the first is that you can de-singletonify your code/library in the future and (hopefully) not break anyone because the method signature remains the same.  You can do it with the second, but it will be less idiomatic.


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Greg Lowe

unread,
Dec 12, 2013, 11:25:00 PM12/12/13
to mi...@dartlang.org
I'm not so worried about how the implementation side looks.

At the use site - I'd say that singletons in Java look more like the second style. The only difference is one is a top level variable vs a static class member in Java (as there are no top level variables). 

Bob Nystrom

unread,
Dec 13, 2013, 1:12:45 PM12/13/13
to General Dart Discussion
On Thu, Dec 12, 2013 at 3:23 PM, Greg Lowe <greg...@gmail.com> wrote:
It seems that the standard style of implementing singletons in Dart is to use factory constructors.

As seen in this answer:

Personally I find the use site "new Singletion()" a bit confusing, as new is not behaving as it normally would.

Oh, dear. This is Python's borg pattern transliterated to Dart. Why would anyone do such a thing?

When I want singleton state in my Dart code, I use this pattern:

var something = "the singleton value";

We have top-level variables for a reason.

Now, of course, I question having static state in general, though. That makes lots of stuff harder, so try to avoid this when you can. But if you do want some static state, just make a variable.

Maybe Seth just wanted to show an interesting example of using factory constructors?

- bob

Justin Fagnani

unread,
Dec 13, 2013, 1:38:39 PM12/13/13
to General Dart Discussion
That may be. Factory constructors are powerful because they allow you to control the instance that's returned and Singleton probably the easiest pattern to apply it to. I find returning a subclass or other implementation of the class more compelling.

In our core libraries and packages I think we use static fields more often, like Zone.ROOT, and top-level when there's naturally a main instance in the environment, like dart:html.window

I'm not sure that's by convention though, or if it just happened.


 

- bob

Greg Lowe

unread,
Dec 13, 2013, 3:35:09 PM12/13/13
to mi...@dartlang.org
Well written chapter. Thanks for sharing.
Reply all
Reply to author
Forward
0 new messages