Search This Blog

Wednesday, 28 September 2016

SingleTon Pattern, how can we restrict in serialization to create multiple objects

SingleTon Pattern :

Questions will be anserwed in this post
What is Singleton
Why do we need SingleTon
How can a class behaves singleTon
How can we fail singleton concept through serialization, how can we solve it..

For a given class, if only one instance needs to be created, we will go for this pattern.
why is SingleTon needed :  there are some requirements, where only one class needs to be created, for example, database connection object,
for an application, only one database is needed, for this kind of situations, we go for singleTon.

 If we restrict constructor as private , no one can create object,
 instead provide one method to give instance, this method will create a object if it didnt exists, else creates object and returns it.
 public class SingleObject {

   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();

   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }

   public void showMessage(){
      System.out.println("Hello World!");
   }
}





Through Serialization, lets say we need to serialize singleTon class, and deserialize n number of times , you can get n number of objects, which is failing singleTon principle, How can we restrict this

In order to make serialization and singleton work properly,we have to introduce readResolve() method in the singleton class.readResolve() method lets developer control what object should be returned  on deserialization.
For the current SingleObject singleton class,readResolve() method will look like this.


     /**
  * Special hook provided by serialization where developer can control what object needs to sent.
  * However this method is invoked on the new object instance created by de serialization process.
  * @return
  * @throws ObjectStreamException
  */
 private Object readResolve() throws ObjectStreamException{
  return INSTANCE;
 }
You need to implement readResolve method in your Singleton class. This small thing is used to override what serialization mechanism has created. What you return there will be used instead of data that came from serialization
 but readResolve will work properly only if all fields are transient,


The best way to do this is to use the enum singleton pattern:

public enum SingleObject {
  instance;
}


more details will be posted further

Tuesday, 27 September 2016

Different ways of creating an Object in Java

Here are the 5 different ways to create object in Java,
1. new Operator
2. Using Class.forName("class").newInstance()
3.Using de serialization  (by using ObjectInputstream readObject method)
4.using clone method
5.using newInstance method.

package com.test.objectcreation;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Add implements Cloneable {
    public int number = 1;

    public int add(int a, int b) {
        return a + b;
    }

    public int change() {
        number = 2;
        return number;
    }

    public static void main(String args[]) throws InstantiationException, IllegalAccessException,
            CloneNotSupportedException, ClassNotFoundException, IOException {
        Add add = new Add(); // creation of object with new operator
        System.out.println(add.add(2, 3));

        Add a = Add.class.newInstance();// creation of object with newInstance
                                        // method
        // System.out.println(a.getName());
        // System.out.println(a.getMethod("add", "2","3"));
        System.out.println(a.add(2, 3));
        a.change();
        Add b = (Add) a.clone(); // creation of object using clone, Class needs to implement clonenable, otherwise you  will get clonenotsupportedException and you need to typecast to class to which you need to clone.
        System.out.println(b.add(2, 3));
        System.out.println(b.toString());
        System.out.println("static int in object a :" + a.number);
        System.out.println(a.toString());
        System.out.println("static int in object b (cloned object) :" + b.number);
        FileInputStream fileIn = new FileInputStream("/E/Add.txt");
        ObjectInputStream in = new ObjectInputStream(fileIn);// here reading serializable object which stored in add.txt file, we are creating Add  classinstance,used mostly for deserialization.

        Add e = (Add) in.readObject();// reading object and type casting.
        in.close();
        fileIn.close();

        Class s = Class.forName("com.test.objectcreation.Add");// using forName
        Add obj = (Add) s.newInstance(); // we are creating object from Object s
        System.out.println(obj.hashCode());
    }

}

Wednesday, 9 March 2016

How can we call Function in Scala

Here are couple of scala points

  • You can call a function without using . operator example : emp getName , Here emp is the object getName is function, just by giving space you can call the function.
  • No need to provide braces after the function Name, unlike in java we use paranthesis (getName()).

Wednesday, 9 December 2015

Ignore JUnit Test Case

How can we stop a test case not to run??


Dont use java comment to comment the test case function,
Please use @Ignore annotation to comment test case,


Example


on function level


@Ignore("not ready yet")
@Test public void testOnething() {
System.out.println("This method is ignored because its not ready yet");
}


on class level

@Ignore public class Ignored {
@Test public void test1() {
System.out.println("Ignored test case 1");
}
@Test public void test2() {
System.out.println("Ignored test case 2");
}
 }

Monday, 7 December 2015

How to turn off interactive prompt while sending bulk files to an ftp server



When we want to send 1000 number of files at a time to an ftp server,

We will use mput *, but for every file it will ask you to enter yes or no to transfer the file,
to avoid this,
use command prompt.

Example below

ftp hostname
username : username
password : password
prompt (this will turn off interactive mode)

now you use mput * to send all files .


Wednesday, 24 September 2014

Hibernate Introduction



  • Hibernate is an ORM (Object Relational Mapping) Tool
  • Hibernate is Open Source
  • Hibernate is used to persist data to Databases
  • Hibernate was started in 2001 by Gavin King
  • ORM is a programming method for mapping the objects to the relational model where entities/classes are mapped to tables,instances are mapped to rows and attributes of instances are mapped to columns of table.
  • Using Hibernate we can save data from Java Applications to Databases. This acts a medium between Java and Database. 

Wednesday, 12 September 2012

Unix Command passwd

One of the Unix Commands which is useful our day to day life is passwd. We will learn now how to use this command.

To change your password this command is useful.
How to change a password for your login:

Login to unix machine with your login id and enter your password
Now if you want to change the current password  passwd command is used to change password.
Type passwd in your command prompt:
Now machine will ask you to enter your current password,please enter it, then it will ask for new password
please enter new password ,the machine will ask again to confirm the new passward please enter it.

Now you changed your password now.