I would like to share some of my views about Doubleton Class in JAVA.
It’s very similar concept to Singleton Design Pattern.
For
any java class if we are allowed to create at-most two objects, such type of
class is called as ‘Doubleton class’.
Before
going forward, please look into the concepts and implementation of SingletonClass.
Example
implementation for the Doubleton class is:
public class Doubleton{
private static Doubleton objDoubleton1;
private static Doubleton objDoubleton2;
private
Doubleton(){
}
//
Factory Method
public static Doubleton getInstance(){
if(objDoubleton1 == null){
objDoubleton1 = new Doubleton();
return objDoubleton1;
}
else if(objDoubleton2 == null){
objDoubleton2 = new Doubleton();
return objDoubleton2;
}
else{
if(Math.random() < 0.5)
return objDoubleton1;
else
return objDoubleton2;
}
}
//
override clone method
public
Object clone(){
return this;
}
}
Any usage scenario for this doubleton design pattern like any example or overview...
ReplyDeletewhat is math.random
ReplyDeleteMath.random will return the value equal to 0.0 or greater than this but less than 1.0.
DeleteVery useful information that you have shared and it is very useful to me. Thanks for sharing the information with us.
ReplyDeletekindle mobi formatting services
public Object clone(){
ReplyDeletereturn this;
}
is clone method has any work with this class doubleton