Core Java Design Pattern : Creational Pattern : Singleton Design Pattern
Singleton Design Pattern |
Ensure a class only has one instance, and provide a global point of access to it.
Example:
It’s important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler.
There should be only one file system and one window manager.
Use the Singleton pattern when
- there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
- when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
It’s important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.
How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn’t keep you from instantiating multiple objects.
A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern.
Examples |
InterestCalculator.java
package com.javaskool.model;
public interface InterestCalculator {
public double calculate(double amt);
}
SavingAccountInterestCalculator.java
package com.javaskool.model;
public class SavingAccountInterestCalculator implements InterestCalculator {
private double roi=3.5;
@Override
public double calculate(double amt) {
// TODO Auto-generated method stub
return (roi/100.0)*amt/12.0;
}
}
FDAccountCalculator.java
package com.javaskool.model;
public class FDAccountCalculator implements InterestCalculator {
private double roi=8.0;
private int duration=3;
@Override
public double calculate(double amt) {
// TODO Auto-generated method stub
return roi/100.0*amt*duration;
}
}
InterestFactory.java
package com.javaskool.factory;
import java.util.HashMap;
import java.util.Map;
import com.javaskool.model.FDAccountCalculator;
import com.javaskool.model.InterestCalculator;
import com.javaskool.model.SavingAccountInterestCalculator;
public class InterestFactory {
private static Map<String , InterestCalculator> maps;
static{
maps=new HashMap<String, InterestCalculator>();
maps.put("s", new SavingAccountInterestCalculator());
maps.put("f", new FDAccountCalculator());
}
public static InterestCalculator create(String type){
System.out.println(maps.get(type));
return maps.get(type);//returning InterestCalculator type
}
private InterestFactory(){}
}
CalculatorService.java
package com.javaskool.service;
import com.javaskool.model.InterestCalculator;
public class CalculatorService {
private InterestCalculator ic;
public CalculatorService(InterestCalculator ic){
this.ic=ic;
}
public double service(double amt){
return ic.calculate(amt);
}
}
TestDrive.java
package com.javaskool;
import com.javaskool.factory.InterestFactory;
import com.javaskool.service.CalculatorService;
public class TestDrive {
public static void main(String arg[]){
//first Object for Saving
CalculatorService servicer=new CalculatorService(InterestFactory.create("s"));
System.out.println("Interest for Savings Account for the amount 10000 " +
"[Current Month]: "+servicer.service(10000));
//using previous object of saving
servicer=new CalculatorService(InterestFactory.create("s"));
System.out.println("Interest for Savings Account for the amount 10000 " +
"[Current Month]: "+servicer.service(10000));
//second object for FD
servicer=new CalculatorService(InterestFactory.create("f"));
System.out.println("Interest for FD Account for the amount 10000 for " +
"3 years: "+servicer.service(10000));
//using previous object of FD
servicer=new CalculatorService(InterestFactory.create("f"));
System.out.println("Interest for FD Account for the amount 10000 for " +
"3 years: "+servicer.service(10000));
}
}
Output
c:\>javac -d . *.java
c:\>java com.javaskool.TestDrive
com.javaskool.model.SavingAccountInterestCalculator@43a30706
Interest for Savings Account for the amount 10000 [Current Month]: 29.16666666666667
com.javaskool.model.SavingAccountInterestCalculator@43a30706
Interest for Savings Account for the amount 10000 [Current Month]: 29.16666666666667
com.javaskool.model.FDAccountCalculator@72b0f2b2
Interest for FD Account for the amount 10000 for 3 years: 2400.0
com.javaskool.model.FDAccountCalculator@72b0f2b2
Interest for FD Account for the amount 10000 for 3 years: 2400.0
Recent Comments