forked from hazukac/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSingleton.java
More file actions
40 lines (33 loc) · 1.12 KB
/
Singleton.java
File metadata and controls
40 lines (33 loc) · 1.12 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
40
// packageがさいしょじゃないといけないっぽい?
package mysingleton;
import java.util.Date;
public class Singleton {
// ここがみそ
private static Singleton instance;
// private Singleton() {}
public String hoge;
public Date initializedAt;
// TODO: otiai10
// プロセスとはなにか、スレッドとはなにか
// TODO: otiai10
// singletonディスりについてしらbてくる
// TODO: otiai10
// 先生ほしい
// defaultだと、同じパッケーじからアクセスできるので
// default Singleton(String x) {
// このクラス定義外で
// new Singleton("hoge");
// という文は書けなくなる
private Singleton(String x) {
this.hoge = x;
this.initializedAt = new Date();
}
// getInstanceでsynchronizedするべきか
// Classのロード時に先にinstanceかしといちゃうか
public static synchronized Singleton getInstance() {
if (Singleton.instance == null) {
Singleton.instance = new Singleton("foo");
}
return Singleton.instance;
}
}