Singleton class:
If any java class allow to create only one object then that
java class is called as “Singleton class”.
Note :-
Singleton class is an idea provided by “Singleton Design
Pattern”.
To prepare a class as Singleton class, we have to use the
following steps .
1.
Declare an user defined class
2.
Declare a private constructor
3.
Provided static factory method with the following
implementation.
a.
Check whether any object is created previously
for the class or not
b.
If any object is existed already then return
that object reference value from static factory method.
c.
If no object is existed previously then create
new object and return the generated new object reference value from static
factory method.
Note :-
To implemented the above logic, we
have to declare a static reference variable of the same class type with null
value and we have to assume if the reference value is null then no object is
existed previously, if the reference variable is not null then there is an
object for the same class previously.
Ex code :-
class A
{
static
A a=null;
private
A()
{
}
static
A getRef()
{
if(a==null)
{
a=new
A();
}
return
a;
}
}
class Test
{
public
static void main(String[] args)
{
System.out.println(A.getRef());
//A@a111
System.out.println(A.getRef());
//A@a111
System.out.println(A.getRef());
//A@a111
}
}
Output:-
A@a111
A@a111
A@a111
Alternative logic for singleton
class
Ex code :-
class A
{
static
A a=null; //a=a111
static
{
a=new
A();
}
private
A()
{
}
static
A getRef()
{
return
a;
}
}
class Test
{
public
static void main(String[] args)
{
System.out.println(A.getRef());
//A@a111
System.out.println(A.getRef());
//A@a111
System.out.println(A.getRef());
//A@a111
}
}
Output:-
A@a111
A@a111
A@a111
Another alternative logic for
Singleton class
Ex code :-
class A
{
static
A a=new A(); //a=a111
private
A()
{
}
static
A getRef()
{
return
a;
}
}
class Test
{
public
static void main(String[] args)
{
System.out.println(A.getRef());
//A@a111
System.out.println(A.getRef());
//A@a111
System.out.println(A.getRef());
//A@a111
}
}
Output:-
A@a111
A@a111
A@a111
In MVC based applications, we will
use a java class as a controller and its must be single as per MVC rules and
regulation. To achieve this requirement, we have to provide a singleton class
as controller class
Ex 1:-
Structs is MVC based framework, where in structs
applications, we have to use ActionServlete a servlet, a java class as
controller, so that, ActionServlete is as singleton class.
Ex 2:-
JSF(java server face) is MVC based
framework, where in JSF applications we have to use faceservlet as controller
and its must be a singleton class.
Ex 3:-
Spring web MVC is an MVC based
framework,, where in spring web MVC application we have to use
“DispatcherServlet” a servlet, a class
as controller and it must be singleton class.
class.forName(--) :
In java application, when we
access constructor along with “new” keyword then JVM will perform the following
two action.
1.
Class loading
2.
Object creating
Ex code:-
class A
{
static
{
System.out.println("Class
Loading");
}
A()
{
System.out.println("Object
Loading");
}
}
class Test
{
public
static void main(String[] args)
{
A
a=new A();
}
}
Output:-
Class Loading
Object Loading
As per the requirement, if we want to load class byte code
to the memory without creating object, then we have to use the following methods.
From java.lang.Class class
public static class.forName(String class_Name)
Ex :-
Class c=Class.forName("A");
When JVM encounter the above instance JVM will perform the
following action.
1.
JVM will take the specified class name from
Class.forName(--) method.
2.
JVM will search for its .class file at current
location, at java predefined library and at the loctions reference by “class
path” environment variable.
3.
If the required .class file is not available at
all the above locations then JVM will rise an exception like
“java.lang.ClassNotFoundException”.
4.
If the required .class file is existed at either
at the above location then JVM will load the respective class byte code to the
memory.
5.
After loading the respective class byte code to
the memory JVM will take meta data by creating “java.lang.Class” object in heap
memory.
In the above context, if we want
to create object for the loading class explicitly. Then we have to use the
following method from “java.lang.Class”.
public Object newInstance()
Ex:-
Object ob=new Instance();
When JVM encounter the above
instruction, JVM will perform the following action.
1.
JVM will go to the loaded class byte code and
search for non-private and 0-org constructor.
2.
If JVM identifiers non-private and 0-org
constructor in the loaded class then JVM will execute that constructor and JVM
will create an object.
3.
In the loaded class, if the constructer is
parameterized constructer then JVM will rise an exception like
“java.lang.InstantitationException”.
4.
In the loaded class, if the constructor is
private constructors then JVM will rise an excerption like
“java.lang.IllegealAccessException”.
5.
In the loaded class, if the constructor is both
private and parameterized constructor then JVM will rise an exception like
“java.lang.InstalizationException”.
Ex code :-
class A
{
static
{
System.out.println("Class
Loading");
}
A()
{
System.out.println("Object
Creating");
}
}
class Test
{
public
static void main(String[] args) throws Exception
{
Class
c=Class.forName("A");
Object
obj=c.newInstance();
}
}
Output:-
Class Loading
Object Creating
Note :-
In JDBC application, we will driver to map JAVA
representation to database representations and database representations to java
representations. In jdbc applications, to make read driver we must load and
register driver to the JDBC application.
Here to load driver class byte code to the memory without
creating object we have to use “Class.forName(--)” method.
Ex:-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Note:-
In client-server
applications, all sun side application arte prepared by using the sever side
components like servlets, JSPs, EJBs,…..
All sever side components are not having main() method, so
that JVM is not Sufficient to execute server side components, where to execute
server side components. We have to use servers, where servers will execute
server side components by using lifecycle actions like “loading”,
“instantiation “and initialization, … while execute server side components by
using lifecycle action like loading and initialization sever will use
“classs.forName()” and c.newInstance()” methods.

No comments:
Post a Comment