Categories
Algorithms & Design Design Pattern

Singleton Pattern

CREATIONAL PATTERN

As its name suggests, singleton pattern is used to maintain single instance of certain class. There can be situations, when we want to create object of class only one time and keep using same object all the time. We will never want to create second object of same class. For example, There is only one sun in our solar system. Hence we cannot have several objects of class named Sun, but only one. In Banking system, each customer account has unique account number. Hence we would wish to design class AccountNumberGenerator. But we would use singleton only one instance of that class, who would take responsibility to generate unique account number for request generated multiple bankers of bank. If we create instances of this class, it may not guarantee uniqueness of account number. Please follow example below how to achieve it technically.

AccountNumberGenerator.java

public class AccountNumberGenerator {

	private static AccountNumberGenerator AccNoInstance = new AccountNumberGenerator();
	private int lastAccountNo;
	
	private AccountNumberGenerator() {
		// TODO Auto-generated constructor stub
		lastAccountNo=0;
	}
	
	public static AccountNumberGenerator getInstance(){
		if(AccNoInstance == null)
			AccNoInstance = new AccountNumberGenerator();
		return AccNoInstance;
	}

	public int getAccountNumber(){
		return (++this.lastAccountNo);
	}
}

Banker.java

public class Banker {

	private String name; 
	public Banker(String name) {
		// TODO Auto-generated constructor stub
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	int providePassbook(){
		int accountNo;
		AccountNumberGenerator instance = AccountNumberGenerator.getInstance();
		accountNo = instance.getAccountNumber();
		return accountNo;
	}
}

SinglePatternDemo.java

public class SingletonPatternDemo {

	public SingletonPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Banker sam = new Banker("Samuel");
		System.out.println("Hey Samuel,I want to open account with your branch");
		System.out.println(sam.getName()+" : Sure sir, we provide you passbook with account No :"+sam.providePassbook());
		
		Banker rajib = new Banker("Rajib");
		System.out.println("Hey rajib,I want to open account with your branch");
		System.out.println(rajib.getName()+" : Sure sir, we provide you passbook with account No :"+sam.providePassbook());
		
		
	}

}

Output

Categories
Design Pattern

What is Design Pattern

Software development is an art of programming. But we don’t always write code from scratch we often copy paste code blocks. For example once we learn how to read text from file, we will continue to apply same logic at point of time later. That means reusing our learning. But is development is also about designs. What if we want to sort numbers, surely we will think about algorithms like bubble sort, heap sort etc. That means we reuse already learned logical solutions to same problem.

Design pattern is also same thing, it is collection of logical solutions to day to day design problems. Factory pattern tells us how to design construction of objects, iterator pattern tells us how to design iterator for iterations etc. Just like algorithms are aimed at minimizing complexity of solutions. Design patterns are aimed at following

  1. Reusability of code – same class or method to be used at multiple places.
  2. Minimum exposure of code to changes. – If we put creation logic in factory class, we will modify factory class only for creation class. No need to touch code where iteration logic is written.

Design patterns are classified into following categories

  1. Creational patterns
  2. Structural patterns
  3. Behavioral patterns

Design a site like this with WordPress.com
Get started