Results 1 to 3 of 3
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,036
    Rep Power
    8

    Default singleton design pattern

    This code presents a counter as a singleton design pattern.
    What is going on with the instanceof() method:
    1) one can call the method
    2) or overrite it, right?

    Who would elaborate on this code concerning the
    "public static synchronized Sequence getInstance()"
    what is the use of it and what is "Lazy instantiation"



    Java Code:
    // Sequence.java public class Sequence {
          private static Sequence instance; 
          private static int counter; 
          private Sequence() {
                 counter = 0; // May be necessary to obtain // starting value elsewhere...
          } 
          public static synchronized Sequence getInstance() {
                    if(instance==null) // Lazy instantiation   {
                            instance = new Sequence(); 
                    }
                   return instance;
          }
     
          public static synchronized int getNext() {
                  return ++counter;
          }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: singleton design pattern

    This is useless. As getNext is static you don't need an instance at all to get the counter. The lazy getter initilializes the instance only once and only if getInstance is called. If you initialze it at the point of declaration, the instance is created at the time the classloader loads Sequence.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,082
    Rep Power
    4

    Default Re: singleton design pattern

    Quote Originally Posted by willemjav View Post
    what is "Lazy instantiation"
    Basically: waiting until the very last moment to create an instance of something, which is generally the first time that you actually need it.

    That is useful in situations where you have a "setup phase". Taking a web environment as an example, the web framework you use will tend to want to initialize some stuff before it can service your web application, so if you would create an object which relies on the framework already being initialized things will go wrong if you instantiate that object for example using a static initializer. If you use lazy initialization however, you have a better opportunity to make things happen in the right order.

Similar Threads

  1. singleton design pattern
    By ziaur25@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2011, 08:28 PM
  2. Using Synchronized keyword in Singleton Design Pattern
    By Cbani in forum Threads and Synchronization
    Replies: 14
    Last Post: 01-08-2010, 04:43 PM
  3. Singleton Pattern
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-24-2008, 03:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •