• Chandrakumar Murugesan

Understanding Dart language using Singleton Design Pattern


As of now, Flutter is not mature enough like Xamarin. But it's going to make big differences in cross-platform application development. No worries, this article is nothing to do with Flutter framework but about its language Dart.


I always have believed that whenever we try to apply a design pattern in a new language like Dart, we can able to understand its good sides of it.

As per Wikipedia, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. Why the singleton pattern? Singleton is a very simple, powerful and most used design pattern.

The following diagram illustrates the most basic structure of a singleton class:

First direct implementation the Java way. It's the pretty straight forward method.

class Singleton {
  static Singleton _instance;
  
  Singleton._private();
  
  static Singleton getInstance() {
    if (_instance == null) {
      _instance = Singleton._private();
    }
    return _instance;
  }

  void sayHello() {
    print('Hello, world');
  }
}

void main(List<String> arguments) {
    Singleton.getInstance().sayHello();
}

Yes, you're correct the Dart doesn't have the modifier as like in Java or C#. Like python, if an identifier or the named constructor(The named constructors to enable a class to define multiple constructors) starts with an underscore _, it’s private to its class(and its library).


Now the Dart way - Version 1

class Singleton {
  static Singleton _instance;
  
  Singleton._private();

  static Singleton getInstance() {
    _instance ??= Singleton._private();
    return _instance;
  }

  void sayHello() {
    print('Hello, world');
  }
}

void main(List<String> arguments) {
    Singleton.getInstance().sayHello();
}

Now the code is simplified by the using Assignment Operator(??=), which check and assign if the null found.


Now using Dart factory constructor - Version 2

class Singleton {
  static Singleton _instance;

  Singleton._();

  factory Singleton() {
    _instance ??= Singleton._();
    return _instance;
  }

  void sayHello() {
    print('Hello, world');
  }
}

void main(List<String> arguments) {
    Singleton().sayHello();
}

I had used factory constructor now. Because a factory constructor, the return type is fixed to the type of the class while for a static method you can provide your own return type. So factory constructor is more relevant here to use it. A factory constructor can be invoked with new, but that became mostly irrelevant with optional new in Dart 2.


I hope version 2 is simpler than the previous two code.


Summary

  1. In dart language, there is no modifier like private, protected, and public. Instead, we can use underscore(_) like python.

  2. Dart provides named constructors to enable a class to define multiple constructors

  3. Assignment Operator ??=, which check and assign if the null found.

  4. The dart language factory constructor, the return type is fixed to the type of the own class return type.


God is Great. All praise to our god.


25 views0 comments

Recent Posts

See All