Rahmadnet

SingleTon

Feb 25th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. 1. what is singleton patten
  2.     Singleton adalah salaha satu konsep dari Desing pttern yang menangani       masalah khususnya pada saat kita membuat instansi dalam sebuah class.
  3. 2. what are their benefit
  4.     Seorang Programmer tidak perlu lagi membuat objek atau instansi berulang kali.
  5. 3. what are their drawback
  6.     Setau saya kelemahan Singleton hanya satu yatu terjadi pemborosan proses pada pengecekan if(instance == null){}.
  7. 4. when to use it at best
  8.     Singleton digunakan apabila kita ingin membatasi bahwa hanya boleh ada sebuah instance class dalam aplikasi.
  9. 5. when to NOT use it
  10.     Singleton tidak digunakan kalau kita tidak ingin membatasi class instance dalam aplikasi.
  11.  
  12. * Class Main
  13. public class ScrabbleTestThread
  14. {
  15.     public static void main(String[] args)
  16.     {
  17.         Runnable getTiles = new GetTheTiles();
  18.         Runnable getTilesAgain = new GetTheTiles();
  19.        
  20.         new Thread(getTiles).start();
  21.         new Thread(getTilesAgain).start();
  22.     }
  23. }
  24.  
  25. * Class Utama
  26. import java.util.Arrays;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.LinkedList;
  30.  
  31. public class Singleton
  32. {
  33.     private static Singleton firtsInstance = null;
  34.    
  35.     String[] scrabbleLetters = {"a","a","a","a","a","a","a","a","a",
  36.                                 "b","b","b","b","b","b","b","b","b",
  37.                                 "c","c","c","c","c","c","c","c","c",
  38.                                 "d","d","d","d","d","d","d","d","d",
  39.                                 "e","e","e","e","e","e","e","e","e",
  40.                                 "f","f","f","f","f","f","f","f","f",
  41.                                 "g","g","g","h","h","h","i","i","i",
  42.                                 "j","j","j","k","k","k","l","l","l",
  43.                                 "m","m","m","n","n","n","o","o","o",
  44.                                 "p","p","p","w","w","w","x","x","x",
  45.                                 "y","y","y","z","z","z","z","z","z"};
  46.                            
  47.     private LinkedList<String> letterList = new LinkedList<String>(Arrays.asList(scrabbleLetters));
  48.    
  49.     static boolean firstThread = true;
  50.    
  51.     private Singleton()
  52.     {
  53.        
  54.     }
  55.    
  56.     public static Singleton getInstance()
  57.     {
  58.         if(firtsInstance == null)
  59.         {
  60.             if(firstThread)
  61.             {
  62.                 firstThread = false;
  63.                
  64.                 Thread.currentThread();
  65.                 try
  66.                 {
  67.                     Thread.sleep(1000);
  68.                 }catch (InterruptedException e)
  69.                 {
  70.                     e.printStackTrace();
  71.                 }
  72.             }
  73.            
  74.             synchronized (Singleton.class)
  75.             {
  76.                 if(firtsInstance == null)
  77.                 {
  78.                     firtsInstance = new Singleton();
  79.                     Collections.shuffle(firtsInstance.letterList);
  80.                 }
  81.             }
  82.            
  83.         }
  84.         return firtsInstance;
  85.     }
  86.        
  87.    
  88.     public LinkedList<String> getLetterList()
  89.     {
  90.         return firtsInstance.letterList;
  91.     }
  92.    
  93.     public LinkedList<String> getTiles(int howManyTiles)
  94.     {
  95.         LinkedList<String> tilesToSend = new LinkedList<String>();
  96.        
  97.         for(int i = 0; i <= howManyTiles; i++)
  98.         {
  99.             tilesToSend.add(firtsInstance.letterList.remove(0));
  100.         }
  101.         return tilesToSend;
  102.     }
  103. }
  104.  
  105. * Class Method
  106.  import java.util.LinkedList;
  107.  
  108. public class GetTheTiles implements Runnable
  109. {
  110.  
  111.     public void run()
  112.     {
  113.         Singleton newInstance = Singleton.getInstance();
  114.         System.out.println("============ID1============");
  115.         System.out.println("Instance ID 1 : "+System.identityHashCode(newInstance));
  116.        
  117.         System.out.println(newInstance.getLetterList());
  118.          
  119.         LinkedList<String> playerOneTiles = newInstance.getTiles(7);
  120.        
  121.        
  122.             System.out.println("Player 1 : " + playerOneTiles);
  123.             System.out.println("Got Tiles");
  124.        
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment