«

»

Jun
29

My First Design Pattern in Java : Singleton

I learned my first design pattern at my oop based java lesson. If you need to create only one instance  and reuse this instance forever (or some classes need only one instance who knows?) you can use this pattern. This pattern was published by GoF.

Where we can use?

Some application need unique items. For example, my company use unique billing system in crm program because we need a unique reference.

How can I use?

First we add private constructor at our claas. It means that we can not use “new” operator at other class. Because when we use “new” operator, it calls class’s constructor for creating instance. Actually, if I can not use new operator at other class and how can I create new instance? Static keyword is the answer of the question. I write a static and public method for getting a new object and then I can create new objects. Then we can count creating of instances via a static integer field (I call it count in my piece of code).  Finally, I use if statement for creating my instance in static getCagdasSingleton() method.

Blueprint of CagdasSingleton class:

 

 

and finally simple java code of my singleton class:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 *
 */
package com.cagdastopcu.designpatterns;
 
/**
 * @author cagdas
 *
 */
public class CagdasSingleton {
 
	private static int count;
	private int nediryani;
	private int index;
	private static CagdasSingleton single;
 
	private CagdasSingleton() {
 
		index = count;
	}
 
	public static CagdasSingleton getCagdasSingleton() {
		// if (single == null)
		if (count++ == 0) // Is it the first creation?
			single = new CagdasSingleton(); // if yes we create our instance
		return single;// if no we use old instance :)
	}
 
	protected void setNediryani(int i) {
		nediryani = i;
	}
 
	protected int getLan() {
		return nediryani;
	}
 
	protected int getIndex() {
		return index;
	}
 
}

 

This is the test class:

 

package com.cagdastopcu.designpatterns;
 
public class CagdasSingletonDriver {
 
	public static void main(String[] args) {
 
		CagdasSingleton mySingleton1 = CagdasSingleton.getCagdasSingleton();
 
		System.out.println(mySingleton1.getIndex());
 
		CagdasSingleton mySingleton2 = CagdasSingleton.getCagdasSingleton();
 
		System.out.println(mySingleton2.getIndex());
 
		CagdasSingleton mySingleton3 = CagdasSingleton.getCagdasSingleton();
 
		System.out.println(mySingleton3.getIndex());
 
		CagdasSingleton mySingleton4 = CagdasSingleton.getCagdasSingleton();
 
		System.out.println(mySingleton4.getIndex());
 
		System.out.println(mySingleton1);
		System.out.println(mySingleton2);
		System.out.println(mySingleton3);
		System.out.println(mySingleton4);
 
	}
 
}

 

Output:

 

cagdas@zanpakutou:$

1
1
1
1
com.cagdastopcu.designpatterns.CagdasSingleton@30c221
com.cagdastopcu.designpatterns.CagdasSingleton@30c221
com.cagdastopcu.designpatterns.CagdasSingleton@30c221
com.cagdastopcu.designpatterns.CagdasSingleton@30c221

 

I tested my singleton design pattern with print reference fields. They point to same heap adress.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre user="" computer="" escaped="">