What is a Doubleton Class ?
For any java class if
we are allowed to create at-most two objects, such type of class is called as ‘Doubleton Class’.
Example
Program 1:
public
class Doubleton1
{
private
static final Doubleton1
instances[] = new Doubleton1[2];
private
static int index;
private
String name;
private
double weight;
private
float length;
static
{
instances[0] = new
Doubleton1("Tiger",
95, 5.5f);
instances[1] = new
Doubleton1("Lion",
85, 5.2f);
}
private
Doubleton1(String
name, double weight, float length) {
this.name = name;
this.weight = weight;
this.length = length;
}
public
static Doubleton1
getInstance() {
return
instances[(index++)%2];
}
@Override
public
String
toString() {
return
"Doubleton1
[name=" + name + ", weight=" + weight + ", length=" + length + "]";
}
}
class
Mgr6 {
public
static void main(String[]
args) {
Doubleton1 obj1 = Doubleton1.getInstance();
Doubleton1 obj2 = Doubleton1.getInstance();
Doubleton1 obj3 = Doubleton1.getInstance();
Doubleton1 obj4 = Doubleton1.getInstance();
Doubleton1 obj5 = Doubleton1.getInstance();
Doubleton1 obj6 = Doubleton1.getInstance();
System.out.println("Printing the
hashcodes to check memory addresses");
System.out.println("obj1.hashCode() :
" + obj1.hashCode());
System.out.println("obj3.hashCode() :
" + obj3.hashCode());
System.out.println("obj5.hashCode() :
" + obj5.hashCode());
System.out.println("obj2.hashCode() :
" + obj2.hashCode());
System.out.println("obj4.hashCode() :
" + obj4.hashCode());
System.out.println("obj6.hashCode() :
" + obj6.hashCode());
System.out.println("Printing the
Object's Attributes");
System.out.println("obj1 : " +
obj1);
System.out.println("obj2 : " +
obj2);
System.out.println("obj3 : " +
obj3);
System.out.println("obj4 : " +
obj4);
System.out.println("obj5 : " +
obj5);
System.out.println("obj6 : " +
obj6);
}
}
Output: Printing
the hashcodes to check memory addresses
obj1.hashCode() : 1671711
obj3.hashCode() : 1671711
obj5.hashCode() : 1671711
obj2.hashCode() : 11394033
obj4.hashCode() : 11394033
obj6.hashCode() : 11394033
Printing
the Object's Attributes
obj1 : Doubleton1 [name=Tiger, weight=95.0,
length=5.5]
obj2 : Doubleton1 [name=Lion, weight=85.0,
length=5.2]
obj3 : Doubleton1 [name=Tiger, weight=95.0,
length=5.5]
obj4 : Doubleton1 [name=Lion, weight=85.0,
length=5.2]
obj5 : Doubleton1 [name=Tiger, weight=95.0,
length=5.5]
obj6 : Doubleton1 [name=Lion, weight=85.0,
length=5.2]
Example
Program 2:
package doubleton ;
public
class Doubleton2
{
private
static Doubleton2
instance1;
private
static Doubleton2
instance2;
private
String name;
private
double weight;
private
float length;
private
Doubleton2(String
name, double weight, float length) {
this.name = name;
this.weight = weight;
this.length = length;
}
public
static Doubleton2
getInstance() {
if(instance1 == null) {
instance1 = new Doubleton2("Cheetah",
120, 5.5f);
return
instance1;
}else
if (instance2 ==
null)
{
instance2 = new Doubleton2("Elephant",
300, 7.2f);
return
instance2;
}else
{
if
(Math.random()
<0.5) {
return
instance1;
} else
{
return
instance2;
}
}
}
@Override
public
String
toString() {
return
"Doubleton2
[name=" + name + ", weight=" + weight+ ", length=" + length + "]";
}
class
Mgr7 {
public
static void main(String[]
args) {
Doubleton2 obj1 = Doubleton2.getInstance();
Doubleton2 obj2 = Doubleton2.getInstance();
Doubleton2 obj3 = Doubleton2.getInstance();
Doubleton2 obj4 = Doubleton2.getInstance();
Doubleton2 obj5 = Doubleton2.getInstance();
Doubleton2 obj6 = Doubleton2.getInstance();
System.out.println("Printing the hashcodes to
check memory addresses");
System.out.println("obj1.hashCode() : "
+ obj1.hashCode());
System.out.println("obj3.hashCode() : "
+ obj3.hashCode());
System.out.println("obj5.hashCode() : "
+ obj5.hashCode());
System.out.println("obj2.hashCode() : "
+ obj2.hashCode());
System.out.println("obj4.hashCode() : "
+ obj4.hashCode());
System.out.println("obj6.hashCode() : "
+ obj6.hashCode());
System.out.println("Printing the Object's
Attributes");
System.out.println("obj1 : " + obj1);
System.out.println("obj2 : " + obj2);
System.out.println("obj3 : " + obj3);
System.out.println("obj4 : " + obj4);
System.out.println("obj5 : " + obj5);
System.out.println("obj6 : " + obj6);
}
}
Output:
Printing
the hashcodes to check memory addresses
obj1.hashCode() : 4384790
obj3.hashCode() : 9634993
obj5.hashCode() : 9634993
obj2.hashCode() : 9634993
obj4.hashCode() : 4384790
obj6.hashCode() : 9634993
Printing
the Object's Attributes
obj1 : Doubleton2 [name=Cheetah, weight=120.0,
length=5.5]
obj2 : Doubleton2 [name=Elephant, weight=300.0,
length=7.2]
obj3 : Doubleton2 [name=Elephant, weight=300.0,
length=7.2]
obj4 : Doubleton2 [name=Cheetah, weight=120.0,
length=5.5]
obj5 : Doubleton2 [name=Elephant, weight=300.0,
length=7.2]
obj6 : Doubleton2 [name=Elephant, weight=300.0, length=7.2]