Ads Sponsored By Google :)

Monday 28 January 2013

guidelines for developing singleton design pattern in java|java2s example|sample code|program|api of


singleton class in java singleton class in java example example of singleton class in java singleton class example in java singleton class in java program singleton class in java api singleton class in java example code singleton class in java2s singleton pattern in java example code singleton design pattern in java example code singleton class sample code in java example of singleton class in java api singleton class in java with example guidelines for developing singleton class in java guidelines for developing singleton class in java example example for guidelines for developing singleton class in java singleton class in java example api example for developing singleton class in java singleton design pattern in java api


       Singleton Class:                   

     Definition:
              Singleton class is a java class which permits   to create single object for JVM.

    Steps / Guide Lines For Developing Singleton Class:
    
       1)   Choose an appropriate package name for placing singleton class and ensure the 
            package statement must be first executable statement.
       2)   Choose an appropriate user defined class and ensure whose modifier must be public 
            and this class is going to be called as singleton class.
      3)   Each and every singleton class must contain self declared object with the nature of private 
             static.
     4)   Each and every singleton class must contain explicitly private default constructor
           (if at all we write any parameterized constructor those constructor’s modifier must be made 
           as private).
     5)   Each and every singleton class must contain factory method by satisfying it’s rules.
     6)   Whatever the single ton class we are placing in the package the class name be given as a 
          filename with an extension (.java).

            In real java JDK we find examples as follows:
              
  // The following program which illustrate the concept of Singleton class.

        Package JavaJavax;// user defined package
  
       Public class Demo
       {
         Private static Demo num=null;
         Private Demo()
        {
          System.out.println(“Object is created first time”);
        }
        public static Demo create()
      {
        if(num==null)
       {
         num=new Demo();
        }
       else
      {
         System.out.println(“Object is already created”);
        }
      }
   }

 Compile the above package program
  C:\> javac –d . Demo.java

  Ensure the package JavaJavax must be created, Demo.java program must be compiled, Demo.class file must be generated and it should be automatically copied into JavaJavax package.
  
    // User Defined Package JavaJavax contains .class file   //Demo.class importing
  // into our user defined class Test.
   //Test.java
   import JavaJavax.Demo;
     Class Test
   {
     Public static void main(String args[])
    {
     // Demo t1=new Demo(); invalid because Test Constructor is  
    //private constructor in JavaJavax.Test class
        Demo t2=Demo.create();// Calling create() From Test class 
                                                   //to create object.
        Demo t3=Demo.create();// Calling create() From Test class   //to create object.
       Demo t4=Demo.create();// Calling create() From Test class //to create object.
    if((t1==t2)&&(t2==t3)&&(t3==t1))
  {
    System.out.println(“all objects are multiple and same”);
  }
else
  {
  System.out.println(“all objects are multiple and different”);
    }
  }
}

Analysis:
      
       The above program makes us to understand an object of JavaJavax.Demo allows us to create only once but not each and every time or even though it allows us to create multiple objects. And all objects have same preference. Hence JavaJavax.Demo class is known as Singleton class.

public class Runtime {
    private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() {
        return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}
Returns the same instance every time and it has a private constructor.




         




No comments:

Post a Comment

LinkWithin