Skip to content

Commit f018d8b

Browse files
committed
singleton added
1 parent c5d8323 commit f018d8b

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

singleton/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Singleton
2+
This is repository of http://androidcode.pl blog. It shows uses of Singleton in Android. It is a part of Design Patterns - Singleton post in the blog.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class DetailsActivity extends AppCompatActivity {
2+
3+
private TextView name, age, isStudent;
4+
5+
@Override
6+
protected void onCreate(Bundle savedInstanceState) {
7+
super.onCreate(savedInstanceState);
8+
setContentView(R.layout.activity_details);
9+
10+
name = findViewById(R.id.name);
11+
age = findViewById(R.id.age);
12+
isStudent = findViewById(R.id.isStudent);
13+
14+
initValues();
15+
}
16+
17+
private void initValues() {
18+
name.setText(SharedPref.getInstance(this).readString(SharedPref.NAME_KEY));
19+
age.setText(String.valueOf(SharedPref.getInstance(this).readInt(SharedPref.AGE_KEY)));
20+
isStudent.setText(String.valueOf(SharedPref.getInstance(this).readBoolean(SharedPref.STUDENT_KEY)));
21+
}
22+
}

singleton/example/MainActivity.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class MainActivity extends AppCompatActivity {
2+
3+
private Button clearPref, setPref, showDetails;
4+
5+
@Override
6+
protected void onCreate(Bundle savedInstanceState) {
7+
super.onCreate(savedInstanceState);
8+
setContentView(R.layout.activity_main);
9+
10+
clearPref = findViewById(R.id.clear_preferences);
11+
setPref = findViewById(R.id.default_preferences);
12+
showDetails = findViewById(R.id.show_details);
13+
14+
clearPref.setOnClickListener(v -> clearPreferences());
15+
setPref.setOnClickListener(v -> setDefaultPreferences());
16+
showDetails.setOnClickListener(v -> showPreferences());
17+
}
18+
19+
private void clearPreferences() {
20+
SharedPref.getInstance(getApplicationContext()).clearPreferences();
21+
}
22+
23+
private void setDefaultPreferences() {
24+
SharedPref.getInstance(getApplicationContext()).writeString(SharedPref.NAME_KEY, "Maciej");
25+
SharedPref.getInstance(getApplicationContext()).writeInt(SharedPref.AGE_KEY, 26);
26+
SharedPref.getInstance(getApplicationContext()).writeBoolean(SharedPref.STUDENT_KEY, false);
27+
}
28+
29+
private void showPreferences() {
30+
Intent intent = new Intent(this, DetailsActivity.class);
31+
startActivity(intent);
32+
}
33+
}

singleton/example/SharedPref.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
public class SharedPref {
2+
3+
public static final String NAME_KEY = "NAME";
4+
public static final String AGE_KEY = "AGE";
5+
public static final String STUDENT_KEY = "IS_STUDENT";
6+
7+
private static SharedPref instance;
8+
private static SharedPreferences sharedPreferences;
9+
private static SharedPreferences.Editor editor;
10+
11+
private SharedPref() {
12+
}
13+
14+
public static SharedPref getInstance(Context context) {
15+
if(instance == null) {
16+
synchronized (SharedPref.class) {
17+
if(instance == null) {
18+
instance = new SharedPref();
19+
sharedPreferences = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
20+
editor = sharedPreferences.edit();
21+
}
22+
}
23+
}
24+
return instance;
25+
}
26+
27+
public void writeString(String key, String value) {
28+
editor.putString(key, value);
29+
editor.commit();
30+
}
31+
32+
public void writeInt(String key, int value) {
33+
editor.putInt(key, value);
34+
editor.commit();
35+
}
36+
37+
public void writeBoolean(String key, boolean value) {
38+
editor.putBoolean(key, value);
39+
editor.commit();
40+
}
41+
42+
public String readString(String key) {
43+
return sharedPreferences.getString(key, "");
44+
}
45+
46+
public int readInt(String key) {
47+
return sharedPreferences.getInt(key, 0);
48+
}
49+
50+
public boolean readBoolean(String key) {
51+
return sharedPreferences.getBoolean(key, false);
52+
}
53+
54+
public void clearPreferences() {
55+
editor.clear();
56+
editor.commit();
57+
}
58+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class DoubleCheckSingleton {
2+
3+
private static DoubleCheckSingleton INSTANCE;
4+
5+
private DoubleCheckSingleton() {
6+
}
7+
8+
public static DoubleCheckSingleton getInstance() {
9+
if (INSTANCE == null)
10+
synchronized (DoubleCheckSingleton.class) {
11+
if (INSTANCE == null)
12+
INSTANCE = new DoubleCheckSingleton();
13+
}
14+
return INSTANCE;
15+
}
16+
}

singleton/pattern/Singleton.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Singleton {
2+
3+
private static Singleton INSTANCE;
4+
5+
private Singleton() {
6+
}
7+
8+
public static Singleton getInstance() {
9+
if(INSTANCE == null) {
10+
INSTANCE = new Singleton();
11+
}
12+
return INSTANCE;
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class SingletonHolder {
2+
3+
private SingletonHolder() {
4+
}
5+
6+
private static class Holder {
7+
private static final SingletonHolder INSTANCE = new SingletonHolder();
8+
}
9+
10+
public static SingletonHolder getInstance() {
11+
return Holder.INSTANCE;
12+
}
13+
}

0 commit comments

Comments
 (0)