-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton_class_example.dart
More file actions
39 lines (31 loc) · 1 KB
/
singleton_class_example.dart
File metadata and controls
39 lines (31 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Example of a singleton class
class DatabaseHelper {
// Singleton databasehelper
static DatabaseHelper? _databaseHelper;
// Named constructor to create an instance
DatabaseHelper._createInstance();
// returns new instance if and only there are not any existing instances!!!
factory DatabaseHelper() {
_databaseHelper ??= DatabaseHelper._createInstance();
return _databaseHelper!;
}
}
// Not a singleton class
class Note {
final int id;
final String title;
Note(this.id, this.title);
}
void main() {
DatabaseHelper databaseHelper1 = DatabaseHelper();
DatabaseHelper databaseHelper2 = DatabaseHelper();
DatabaseHelper databaseHelper3 = DatabaseHelper();
print(databaseHelper1.hashCode);
print(databaseHelper2.hashCode);
print(databaseHelper3.hashCode);
Note note1 = Note(1, "First Note");
Note note2 = Note(2, "Second Note");
// as they are not maintaining singleton pattern therefore their hashCode will be different
print(note1.hashCode);
print(note2.hashCode);
}