Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. what is singleton patten
- Singleton adalah salaha satu konsep dari Desing pttern yang menangani masalah khususnya pada saat kita membuat instansi dalam sebuah class.
- 2. what are their benefit
- Seorang Programmer tidak perlu lagi membuat objek atau instansi berulang kali.
- 3. what are their drawback
- Setau saya kelemahan Singleton hanya satu yatu terjadi pemborosan proses pada pengecekan if(instance == null){}.
- 4. when to use it at best
- Singleton digunakan apabila kita ingin membatasi bahwa hanya boleh ada sebuah instance class dalam aplikasi.
- 5. when to NOT use it
- Singleton tidak digunakan kalau kita tidak ingin membatasi class instance dalam aplikasi.
- * Class Main
- public class ScrabbleTestThread
- {
- public static void main(String[] args)
- {
- Runnable getTiles = new GetTheTiles();
- Runnable getTilesAgain = new GetTheTiles();
- new Thread(getTiles).start();
- new Thread(getTilesAgain).start();
- }
- }
- * Class Utama
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.LinkedList;
- public class Singleton
- {
- private static Singleton firtsInstance = null;
- String[] scrabbleLetters = {"a","a","a","a","a","a","a","a","a",
- "b","b","b","b","b","b","b","b","b",
- "c","c","c","c","c","c","c","c","c",
- "d","d","d","d","d","d","d","d","d",
- "e","e","e","e","e","e","e","e","e",
- "f","f","f","f","f","f","f","f","f",
- "g","g","g","h","h","h","i","i","i",
- "j","j","j","k","k","k","l","l","l",
- "m","m","m","n","n","n","o","o","o",
- "p","p","p","w","w","w","x","x","x",
- "y","y","y","z","z","z","z","z","z"};
- private LinkedList<String> letterList = new LinkedList<String>(Arrays.asList(scrabbleLetters));
- static boolean firstThread = true;
- private Singleton()
- {
- }
- public static Singleton getInstance()
- {
- if(firtsInstance == null)
- {
- if(firstThread)
- {
- firstThread = false;
- Thread.currentThread();
- try
- {
- Thread.sleep(1000);
- }catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- synchronized (Singleton.class)
- {
- if(firtsInstance == null)
- {
- firtsInstance = new Singleton();
- Collections.shuffle(firtsInstance.letterList);
- }
- }
- }
- return firtsInstance;
- }
- public LinkedList<String> getLetterList()
- {
- return firtsInstance.letterList;
- }
- public LinkedList<String> getTiles(int howManyTiles)
- {
- LinkedList<String> tilesToSend = new LinkedList<String>();
- for(int i = 0; i <= howManyTiles; i++)
- {
- tilesToSend.add(firtsInstance.letterList.remove(0));
- }
- return tilesToSend;
- }
- }
- * Class Method
- import java.util.LinkedList;
- public class GetTheTiles implements Runnable
- {
- public void run()
- {
- Singleton newInstance = Singleton.getInstance();
- System.out.println("============ID1============");
- System.out.println("Instance ID 1 : "+System.identityHashCode(newInstance));
- System.out.println(newInstance.getLetterList());
- LinkedList<String> playerOneTiles = newInstance.getTiles(7);
- System.out.println("Player 1 : " + playerOneTiles);
- System.out.println("Got Tiles");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment