Flutter/Dart: Design Pattern- Singleton

Jitesh Mohite
FlutterWorld
Published in
2 min readOct 2, 2021

Design patterns are important when it comes to software development, and choosing the right one gives us a lot of headaches. Singleton pattern comes under Creational Design Patterns, which says

In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

Singleton pattern is useful to restrict the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system.

It seems to be a simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns. The implementation of the Singleton pattern has always been a controversial topic among developers. Here, We are going to discuss how to create a Singleton class that fulfills its purpose :

There are two ways to create a Singleton class:

  1. Eager Initialization
  2. Lazy Initialization

So, Let's dive inside now

Eager Initialization:

This is the simplest approach for creating a singleton class, In this instance of the class is created at the time of class loading which is done by DVM(Dart Virtual Machine). It can be used when the program will always use an instance of this class, or the cost of creating the instance is not too large in terms of resources and time.

class Singleton {
static Singleton _instance = Singleton._();

Singleton._();

static Singleton get instance => _instance;
void someMethod() {
print("someMethod Called");
}
}

Lazily Initialization:

In this method, the object is created only if it is needed. This may prevent resource wastage. An implementation of the Singleton() the method is required which returns the instance. To make sure that class cannot be instantiated in any other way, the constructor is made private. As the object is created within a method, it ensures that the object will not be created until and unless it is required.

class Singleton {
static Singleton? _instance;

Singleton._internal();

factory Singleton() => _instance ??= Singleton._internal();

void someMethod() {
print("someMethod Called");
}
}

Pros:

  • Restrict the number of instances, which ensures that a class can have only one instance, throughout the application.
  • Access a global variable, anywhere required.

Cons:

  • Unit test cases required loosely coupled classes, in order to test write better test cases, as they maintain a global state of variables it becomes difficult to test specific scenarios. The solution would be to create parameterized constructor/method to pass singleton objects.
  • It violates the Single Responsibility Principle, as it maintains the global state through the app.

--

--

Jitesh Mohite
FlutterWorld

I am technology enthusiastic, want to learn things quickly and dive deep inside it. I always believe in developing logical things which makes impact on end user