class Peoples {
late int id;
late String name;
static final Peoples _inst = Peoples._internal();
Peoples._internal();
factory Peoples() {
return _inst;
}
}
I have this singleton class. Which ensures that, only one instance of a class is ever created. So, even if someone tries to instantiate it, they will use the same instance. And i can create and set values, like:
Peoples ps1 = Peoples();
Peoples ps2 = Peoples();
ps1.id = 1;
ps1.name = "First";
ps2.id = 2;
ps2.name = "Second";
Is it possible to instantiate and set values like:
Peoples ps1 = Peoples(1, "First");
Peoples ps2 = Peoples(2, "Second");
So, now both "ps1" and "ps2" will have (2, "Second").
In case of singleton classes there is no use of static methods as there is only one instance available of the class and every buddy is having the same copy of it.
A common mistake with that implementation is to neglect synchronization, which can lead to multiple instances of the singleton class.
The singleton pattern is a pattern used in object-oriented programming which ensures that a class has only one instance and also provides a global point of access to it. Sometimes it's important for a class to have exactly one instance, or you might force your app into a weird state.
The only way to override a singleton is to have a singleton that expects to be overridden. The simplest way to do this is to provide Singleton that implements an interface (or is otherwise fully abstract itself) that internally instantiates an injected singleton upon the first use of getInstance() .
Sure! You need to pass the arguments to the factory method then you need to update the properties USING the referenced instance.
For example, you had
class Peoples {
int id;
String name;
static final Peoples _inst = Peoples._internal();
Peoples._internal();
factory Peoples() {
return _inst;
}
}
If you apply my solution then you have
class Peoples {
int id;
String name;
static final Peoples _inst = Peoples._internal();
Peoples._internal();
factory Peoples({int id, String name}) {
_inst.id = id
_inst.name = name
return _inst;
}
}
with this your question should be answered for more info about factory and params visit
https://dart.dev/guides/language/language-tour
Working Example
class Peoples {
int id;
String name;
static final Peoples _inst = Peoples._internal();
Peoples._internal();
factory Peoples(int id, String name) {
_inst.id = id;
_inst.name = name;
return _inst;
}
}
void main() {
print("Instance of = " + Peoples(0, "Dylan").name);
print("Instance of = " + Peoples(1, "Joe").name);
print("Instance of = " + Peoples(2, "Maria").name);
}
I'd like to answer showing a way to create a singleton by passing arguments to it and how to "lock" its values after creating it for the first time.
class People {
static final People _inst = People._internal();
People._internal();
factory People(int id, String name) {
assert(!_inst._lock, "it's a singleton that can't re-defined");
_inst.id = id;
_inst.name = name;
_inst._lock = true;
return _inst;
}
int id;
String name;
bool _lock = false;
}
void main() {
var people = People(0, 'Dylan');
try{
print('Instance of = ' + People(0, 'Joe').name);
print('Instance of = ' + People(1, 'Maria').name);
print('Instance of = ' + People(2, 'Ete sech').name);
} finally {
print('Instance of = ' + people.name);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With