SINGLETON AND FACTORY PATTERN
patterns often defined as many ways of providing communication between different entities in an object-oriented manner, here I have given example of two basic patterns.
- Singleton pattern.
This pattern is the simplest of all the patterns out there. the main aim is to make sure that we create no more than one instance of our class we do this by making our constructor as private. The below code gives the lazy initialization method.
public class SingletonClass {
private static SingletonClass SINGLE_INSTANCE = null;
private SingletonClass() {}
public static SingletonClass getInstance() {
if (SINGLE_INSTANCE == null) {
synchronized(SingletonClass.class) {
SINGLE_INSTANCE = new SingletonClass();
}
}
return SINGLE_INSTANCE;
}
}
2. Factory Pattern
The factory pattern is one of the most used design patterns in Java. This type of design pattern comes under a creational pattern as this pattern provides one of the best ways to create an object.
In the Factory pattern, we create an object without exposing the creation logic to the client and refer to the newly created objects using a common interface.
FACTORY PATTERN USING INTERFACE
//INTERFACE
public interface Shape {
void draw();
}
//RECTANGLE CLASSpublic class Rectangle implements Shape { public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}//SQUARE CLASSpublic class Square implements Shape {
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
//CIRCLE CLASSpublic class Circle implements Shape {
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}//SHAPE FACTORY CLASS WHERE ALL THE INSTANATION OF CLASS IS DONEpublic class ShapeFactory {
//use getShape method to get object of type shape
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
//THE MAIN CLASS WHERE ALL THE OBJECT CALL AND i/p o/p is donepublic class FactoryPatternDemo {public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory(); //get an object of Circle and call its draw method.
Shape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Circle
shape1.draw(); //get an object of Rectangle and call its draw method.
Shape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Rectangle
shape2.draw(); //get an object of Square and call its draw method.
Shape shape3 = shapeFactory.getShape("SQUARE"); //call draw method of square
shape3.draw();
}
}THE OUTPUT:Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
FACTORY PATTERN USING ABSTRACT CLASS
import java.io.*;
abstract class Plan{
protected double rate;
abstract void getRate();public void calculateBill(int units){
System.out.println(units*rate);
}
}class DomesticPlan extends Plan{
//@override
public void getRate(){
rate=3.50;
}
}class CommercialPlan extends Plan{
//@override
public void getRate(){
rate=7.50;
}
}class InstitutionalPlan extends Plan{
public void getRate(){
rate=5.50;
}
}class GetPlanFactory{
//use getPlan method to get object of type Plan
public Plan getPlan(String planType){
if(planType == null){
return null;
}
if(planType.equalsIgnoreCase("DOMESTICPLAN")) {
return new DomesticPlan();
}
else if(planType.equalsIgnoreCase("COMMERCIALPLAN")){
return new CommercialPlan();
}
else if(planType.equalsIgnoreCase("INSTITUTIONALPLAN")) {
return new InstitutionalPlan();
}
return null;
}
}public class pattern{
public static void main(String args[])throws IOException{
GetPlanFactory planFactory = new GetPlanFactory();
String planName="DOMESTICPLAN"; int units=10;
Plan p = planFactory.getPlan(planName);
System.out.print("Bill amount for "+planName+" of "+units+" units is: ");
p.getRate();
p.calculateBill(units);
}
}