FlutterWorld
Published in

FlutterWorld

Flutter/Dart: Design Pattern- Singleton

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.

--

--

--

The fastest growing community which makes development easier

Recommended from Medium

HMS Account Kit and Features

Launch an Application using EFS and VPC in AWS

How to Create a Simple Cookie Consent Pop-up for your Website

Learn Git Within 5 Minutes

Internal working of get(key) and put(key,value) in HashMaps

A Case Study About Identity Management

Superalgos Review — A 1 Month Review (novice)

Development Process — Summer Internship 2020

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jitesh Mohite

Jitesh Mohite

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

More from Medium

Flutter — BLoC Architecture

Add Multi languages to your app using GetX

Thumbnail about this tutorial content

Flutter Introduction Screen [Intro slides]

Bottom Navigation Bar in Flutter