1 / 21

Singleton Pattern

Singleton Pattern. 1. uno. one. What is Singleton ?. One object of a kind Ada class yang hanya perlu diinstansiasi 1 kali saja Alasan : Dalam beberapa kasus hanya dibutuhkan 1 object saja. Like What ?. Database Connection. Dalam banyak kasus , koneksi database cukup satu saja

cora
Download Presentation

Singleton Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Singleton Pattern 1 uno one

  2. What is Singleton ? • One object of a kind • Ada class yang hanyaperludiinstansiasi 1 kali saja • Alasan: Dalambeberapakasushanyadibutuhkan 1 object saja

  3. Like What ? • Database Connection Dalambanyakkasus, koneksi database cukupsatusaja Misal : hanya 1 lisensikoneksi

  4. Else ? • Application Setting (Preferences) Beberapabagian program yang berbedaakanmembacadanmenulis setting

  5. More ? • Logging activities Beberapabagian program perlumenuliske file log yang sama

  6. Singleton • Memastikansuatu class hanyadapat “menghasilkan” satu object saja • Konsepnyamirip global variable (denganperbedaan) • Pada Singleton, object hanyadibuatsaatdibutuhkan, sedangkan Global Variable dibuatsaatawal program dijalankan

  7. Public ! Bagaimanajika default ? public class Whatever { … } Bagaimanacaramembuat object dari class Whatever ? Whatever object1 = new Whatever(); Bisamembuatlebihdari 1 object ? Whatever object1 = new Whatever(); Whatever object2 = new Whatever(); … Whatever object100 = new Whatever();

  8. Hanyadapatdiaksesdari package yang sama ! class Whatever { … } Tetapbisadibuatlebihdari 1 object dari class Whatever Whatever object1 = new Whatever(); Whatever object2 = new Whatever(); … Whatever object100 = new Whatever(); Object membutuhkan memory, semakinbanyak object ?

  9. Tahukahanda, kitadapatmelakukanini public class Whatever { private Whatever() { } } Private Constructor ! Apakonsekuensinya ? Hubungannyadengan Singleton ?

  10. Masihingatkonsepstatic method ? public class Whatever { public static void hitung() { … } } Method hitung() dapatdipanggiltanpaperlumelakukan instansiasi class Whatever Whatever.hitung();

  11. Bagaimanajikasepertiini ? public class Whatever { public static Whatever getInstance() { … } } Method tersebutmengembalikan object bertipe Whatever Whatever object1 = Whatever.getInstance();

  12. Kalauini ? public class Whatever { private Whatever() { } public static Whatever getInstance() { return new Whatever(); } } Tapimasihbisamembuatlebihdari 1 object !! Memanggil static method getInstance() merupakancara alternatifuntukinstansiasi

  13. public class Singleton { private static Singleton oneObject; private Singleton() { } //… public static Whatever getInstance() { if(oneObject == null) oneObject= new Singleton(); return oneObject; } //…. }

  14. Sudahcukup? TIDAK !! public static Whatever getInstance() { if(oneObject == null) oneObject= new Singleton(); return oneObject; } Berpotensi “gagal” jika program kita Multithreading !

  15. Easy Fix ! public static synchronized Whatever getInstance() { if(oneObject == null) oneObject= new Singleton(); return oneObject; } Cara inimemilikikelemahankarena synchronizedmembuataplikasianda menjadilebihlambat !!

  16. BeberapaPilihan (Multithread) • Tetapgunakan synchronized • Jika proses pembuatan object tidakmembutuhkansumberdaya yang besar (waktudan memory) • Buat instance sedinimungkin • Dijamin “aman” untuk multithread ! • Lakukanpengecekan “ganda” !

  17. Buat instance sedinimungkin (Thread safe) public class Singleton { private static Singleton oneObject = new Singleton(); private Singleton() { } //… public static Whatever getInstance() { return oneObject; } //…. }

  18. public class Singleton { private volatile static Singleton oneObject; private Singleton() { } //… public static Whatever getInstance() { if(oneObject == null) synchronized(Singleton.class) { if(oneObject == null) { oneObject = new Singleton(); } } } //…. } Pengecekanganda !

  19. Class Diagram Singleton

  20. SEGERA  Ada pertanyaan ?Konsultasikankomponenanda

  21. Next : command pattern

More Related