-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingleton.java
47 lines (37 loc) · 1.14 KB
/
singleton.java
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
41
42
43
44
45
46
47
class Singleton {
private static Singleton instance = null;
private static String instance_attr1 = "";
private static String instance_attr2 = "";
public static String getInstance_attr1() {
return instance_attr1;
}
public static void setInstance_attr1(String instance_attr1) {
Singleton.instance_attr1 = instance_attr1;
}
public static String getInstance_attr2() {
return instance_attr2;
}
public static void setInstance_attr2(String instance_attr2) {
Singleton.instance_attr2 = instance_attr2;
}
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
public void show(){
System.out.printf("attr1: %s, attr2: %s\n", getInstance_attr1(), getInstance_attr2());
}
}
public class Main {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
singleton.show();
Singleton.setInstance_attr1("apple");
Singleton.setInstance_attr2("banana");
singleton.show();
}
}