Skip to content

Commit b2143cd

Browse files
committed
Impl Singleton pattern.
1 parent c46cc84 commit b2143cd

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'pattern/singleton.dart';
2+
3+
void main() {
4+
// dart style
5+
Singleton().doSome();
6+
Singleton().doSome();
7+
8+
// standard style
9+
Singleton.instance.doSome();
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
abstract class Singleton {
2+
factory Singleton() {
3+
if (_instance == null) {
4+
print('Create singleton once.');
5+
_instance = ConcreteSingleton();
6+
}
7+
8+
return _instance!;
9+
}
10+
11+
static Singleton get instance => Singleton();
12+
13+
void doSome();
14+
15+
static Singleton? _instance;
16+
}
17+
18+
class ConcreteSingleton implements Singleton {
19+
@override
20+
void doSome() {
21+
print('doSome()');
22+
}
23+
}

0 commit comments

Comments
 (0)