Friday 5 July 2019

Design Patterns | Modified Singleton Design pattern

Make a doubleton which returns the first object in even call of getInstance()  and second instance for odd call. See code below:

public class Doubleton {
      private Doubleton() {

      }

      private static int call = 0;
      private static Doubleton[] instances = { new Doubleton(), new Doubleton() };

      public static Doubleton getInstance() {
            return instances[call++ % 2];
      }

      public static void main(String[] args) {
            Doubleton d1 = Doubleton.getInstance();
            Doubleton d2 = Doubleton.getInstance();

            for (int i = 0; i < 10; i++) {
                  if (i % 2 == 0) {
                        System.out.println(d1 == Doubleton.getInstance());
                  } else {
                        System.out.println(d2 == Doubleton.getInstance());
                  }
            }
      }
}


See Thread Safe code below:

public class Doubleton {

      private static volatile Doubleton INSTANCE1;
      private static volatile Doubleton INSTANCE2;

      private static int call = 0;

      private Doubleton() {

      }
      public static Doubleton getInstance() {
            if (call++ % 2 == 0) {
                  if (INSTANCE1 == null) {
                        synchronized (Doubleton.class) {
                              if (INSTANCE1 == null) {
                                    INSTANCE1 = new Doubleton();
                              }
                        }
                  }
                  return INSTANCE1;
            } else {
                  if (INSTANCE2 == null) {
                        synchronized (Doubleton.class) {
                              if (INSTANCE2 == null) {
                                    INSTANCE2 = new Doubleton();
                              }
                        }
                  }
                  return INSTANCE2;
            }
      }
}

Sunday 30 June 2019

Exceptions | Difference between ArrayIndexOutfOBounds and ArrayStoreException

ArrayIndexOutOfBoundsException
Since JDK1.0

ArrayIndexOutOfBoundsException occurs when your code tries to access an invalid index for a given array e.g. negative index or higher index than length - 1.

java.lang.Object
   java.lang.Throwable
      java.lang.Exception
         java.lang.RuntimeException
            java.lang.IndexOutOfBoundsException
               java.lang.ArrayIndexOutOfBoundsException


  • ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException.
  • Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

ArrayStoreException
Since JDK1.0
ArrayStoreException occurs when you have stored an element of type other than the type of array.

java.lang.Object
   java.lang.Throwable
      java.lang.Exception
         java.lang.RuntimeException
            java.lang.ArrayStoreException


  • ArrayStoreException extends RuntimeException
  • Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.

Example
Object x[] = new String[3];
x[0] = new Integer(0);

Related Posts Plugin for WordPress, Blogger...