Singleton Design Pattern คืออะไร?
Singleton เป็นรูปแบบการออกแบบ (Design Pattern) ที่จำกัดการสร้างอินสแตนซ์ของคลาสให้มีเพียงอินสแตนซ์เดียวเท่านั้น ใช้สำหรับควบคุม Concurrency และสร้างจุดเข้าถึงศูนย์กลางสำหรับแอปพลิเคชัน
ตัวอย่าง: การใช้ Singleton สำหรับ Notification
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project → กรอกรายละเอียด
ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/show"
android:text="Show notification from Singleton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ขั้นตอนที่ 3: เขียน MainActivity.java
package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button show;
SingleTonExample singletonExample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
// เรียกใช้ Singleton instance
singletonExample = SingleTonExample.getInstance();
singletonExample.init(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
singletonExample.showNotification(MainActivity.this);
}
});
}
}
ขั้นตอนที่ 4: สร้างคลาส Singleton (SingleTonExample.java)
package com.example.andy.myapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
public class SingleTonExample {
public static final String ANDROID_CHANNEL_ID = "com.example.andy.myapplication";
private static SingleTonExample ourInstance = new SingleTonExample();
private Context appContext;
// Constructor เป็น private เพื่อป้องกันการสร้างจากภายนอก
private SingleTonExample() { }
public static synchronized SingleTonExample getInstance() {
return ourInstance;
}
public void init(Context context) {
if (appContext == null) {
this.appContext = context;
}
}
private Context getContext() {
return appContext;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void showNotification(final MainActivity mainActivity) {
NotificationManager notif = (NotificationManager) mainActivity.getSystemService(Context.NOTIFICATION_SERVICE);
// สร้าง Notification Channel (จำเป็นสำหรับ Android Oreo+)
NotificationChannel androidChannel = new NotificationChannel(
ANDROID_CHANNEL_ID,
"Notification Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
androidChannel.enableLights(true);
androidChannel.enableVibration(true);
androidChannel.setLightColor(Color.GREEN);
androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
notif.createNotificationChannel(androidChannel);
// สร้างและแสดง Notification
Notification notify = new Notification.Builder(mainActivity, ANDROID_CHANNEL_ID)
.setContentTitle("Notification")
.setContentText("Sample Text")
.setSubText("Sample Subject")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
notif.notify(0, notify);
}
}
การทดสอบและผลลัพธ์


ข้อดีของการใช้ Singleton สำหรับ Notification
- ประหยัดหน่วยความจำ - มีเพียงอินสแตนซ์เดียวตลอดวงจรชีวิตของแอป
- จัดการ Notification Channel ได้อย่างมีประสิทธิภาพ (สร้างเพียงครั้งเดียว)
- เข้าถึงได้จากทุกที่ในแอปผ่าน
SingleTonExample.getInstance() - ป้องกันการสร้าง Notification ซ้ำซ้อน
สรุป
Singleton Pattern เหมาะสำหรับการจัดการ Notification เพราะ NotificationManager และ NotificationChannel ควรมีเพียงอินสแตนซ์เดียวในแอปพลิเคชัน