Saturday, February 12, 2011

Singleton Pattern

Singleton pattern is a kind of creational pattern that deals with the object creation.the intention of the singleton pattern is to limit and control the unnecessary objects creation.therefore it will help to reduce the unnecessary resource usage.singleton pattern ensures that the class has only one instance and it will be globally accessed throughout the application. this pattern can be applied for a situation where the only a single instance of a class is required throughout the application life-cycle.


Singleton Pattern implementation in PHP 

SingletonClass.php


class SingletonClass{


    private static $singletonClassObject = null;

    //  private function __construct() {}

    public static function getSingletonInstance(){

        if(self::$singletonClassObject==null){

            //creating new object
            self::$singletonClassObject = new SingletonClass();

        }

        return self::$singletonClassObject;

    }//getSingletonInstance


}//SingletonClass



the below code represents the client who tries to receive the singleton instance of the above class.


//get the object first time
echo "get the SingletonClass object first time  [".spl_object_hash(SingletonClass::getSingletonInstance())."]<br/>";

//get the object second time
echo "get the SingletonClass object second time [".spl_object_hash(SingletonClass::getSingletonInstance())."]<br/>";



you can see that the both it gets the objects with the same object reference id. that means both invocation leads to get the access for the same object.(not the two different objects). that is why the both time, you get the same object reference.


Singleton Pattern implementation in JAVA

SingletonClass.java
 

public class SingletonClass {

    private static SingletonClass singletonObject = null;


    public static SingletonClass getInstance(){

        if(singletonObject==null){
            //create the object
            singletonObject = new SingletonClass();
        }

        return singletonObject;

    }//getInstance
}//SingletonClass

Tuesday, February 8, 2011

Installaing Apache Tomcat in Windows Platform

Today i am going to discuss how to install Apache Tomcat server in the windows platform. i think this post will be helpful to most of the people ho are new to java(specially J2EE) developments. i decided to compose this article because the most of my friends were confusing and messing the things up when installing and configuring the Apache tomcat server. I will be installing Apache tomcat 7 in my PC.

prerequisite for Apache Tomcat 7:
JDK 1.6 or higher version

you can download the Apache tomcat 7 or latest through the Apache Software Foundation official website for Tomcat server. i have downloaded the binary distribution version for the installation.

then follow the installation instructions as below.

1. extract the ZIP file and copy the  extracted apache-tomcat-7.0.23 directory to the C:\Program Files

2. then create the following environmental variables.

 JAVA_HOME(the value should refers the java installation directory e.g:- C:\Program Files\Java\jdk1.6.0)

CATALINA_HOME(the value should refers the Apache tomcat extracted directory e.g:- C:\Program Files\apache-tomcat-7.0.23)


3. now  the tomcat server is installed  and you can run and test the server as follows.
   run the startup.bat file that is inside the bin directory of the tomcat server  using the windows command prompt(cmd). then it will startup and run the server.

4. browse http://localhost:8080 url and you will be able to see apache server is running.

(Note: by default, the apache tomcat server is running on port 8080. if you need to change the port it can be done by changing the server port declaration done in the server.xml file located inside the conf directory (conf/server.xml))

Hope this will helpful for you!

regards
Chathuranga Tennakoon
chathuranga.t@gmail.com