"Diethelm Guallar, Gonzalo" wrote:
> 1. A "system" object that must be alive while the server is
>    processing requests; a Singleton, used by all interactions.

> The question is, what is a good way to handle (1)? Where do I
> put the line
> 
>   System system = new System(...);

You answered your question yourself - when your object is a
signleton it is created when getInstance() is called, this is
the first time any service is requested from your object.

Here is an example of Singleton patter for you :
(you must have seen many of them in Turbine sources)

public class System 
{
    // clients must use getInstance() to get an instnce of this class
    private System() 
    {
        // initialize the System
    }

    // the single instance of this class
    private static instance;    

    public static System getInstance()
    {
        // make sure you don't synchronize duing each request for instance
        // synchronization takes time, and the instance usually exists.
        if(instance == null)
        {
            // acquire a sandbox (e.g. Tomcat context) wide lock
            // to prevent two threads from performing initialization concurrently
            synchronized(System.class)
            {
                // check instance again, because other thread could enter the
                // monitor AFTER the above if. checking again will be usually
                // faster than extraneous calling the constructor. On the other
                // hand calling the constructor twice might break something.
                if(instance == null)
                {
                    // initialize the System now
                    instance = new Instance();
                }
            }
        }
        // return the valid instance to the client.
        return instance;
    }
}

every time you need to call a method from your System:

System.getInstance().method(args);

and the pattern guarantees that System will be always initialized, once per life of 
the VM.


Rafal


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to