Singleton Class in Java and How To Use in Selenium - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Friday 30 March 2018

Singleton Class in Java and How To Use in Selenium


A class for which we can create only one Object as the name itself suggests its a singleton Class

Runtime is a singleton class

We dont want too many objects to be created so that memory utilization would be less and performance should be improved.

So we allow only Object for Runtime class and reuse the same for other purpose as well

Generally we make such classes as Singleton for whom the purpose of creating object is same everytime. So no need to  create new Object everytime rather use the existing one.

Singleton classes can't be created using Constructors so we don't use new here

We use Factory methods for creating object of Singleton Classes

Example
package mypackage;



import java.util.Date;



public class MySingleTon {

 

 public static void main(String[] args) {

  

  Runtime r=Runtime.getRuntime(); // Singleton Class so we dont use new here

  

  Runtime r1=Runtime.getRuntime();

  

  Date d=new Date(); // Date is not Singleton so we use new here

  Date d1=new Date();

  

  System.out.println(r==r1); //true as r and r1 would be same object in case of RunTime

  System.out.println(d==d1);

 }

}
Output
true
false

We make the constructor as Private so that no one can make object from outside

Example of SingleTon Class

package mypackage;
public class MySingleTon {

private static MySingleTon obj=new MySingleTon(); // we created Object private
private MySingleTon(){// Constructor private

}

          // Factory Method
public static MySingleTon MySingleTonFunction(){ // we create this function as Public so that anyone can access and create object using this function not using 'new' operator
// we are returning already created object using obj above using new operator above
//moreover that object is also static

return obj;
}
public static void main(String[] args) {

MySingleTon obj1=MySingleTon.MySingleTonFunction();
MySingleTon obj2=MySingleTon.MySingleTonFunction();

System.out.println(obj1==obj2);// it returs true ot means no matter how many object u create it will create only one object internally
}
}

Output

true

Note:
Always we use  Class name.functionname to create a new Object in Singleton.
because concept of static says call function name from class by . operator

Example above

MySingleTon.MySingleTonFunction();

Observation
For Single Ton class we have to use
1.)  Private Constructor
2.) Static private variable
3.) Public factory method

Factory Method are methods which release same class memory or other class memory are called Factory Methods // Factory release same products always so can say name has been given Factory

Second Approach

Initialization-on-demand or Lazy Initialization

package mypackage;

public class MySingleTon {

 

 private static MySingleTon obj=null; // we created Object private // This approach is also called //Lazy instantiation

 private MySingleTon(){// Constructor private

  

 }

 

 public static MySingleTon MySingleTonFunction(){ // we create this function as Public so that anyone can access and create object using this function not using 'new' operator

  // we are returning already created object using obj above using new operator above

  //moreover that object is also static

  

  if(obj==null){

   obj=new MySingleTon();

  }

  return obj;

 }

 public static void main(String[] args) {

  

  MySingleTon obj1=MySingleTon.MySingleTonFunction();

  MySingleTon obj2=MySingleTon.MySingleTonFunction();

  

  System.out.println(obj1==obj2);

 }

}





Lazy Initialization-->  the instance of a class is created when its required to be used for the first time. The idea behind this is to avoid unnecessary instance creation.


BaseTest Class

public class BaseTest{

 public static  WebDriver driver=null;

 public WebDriver getDriver(String browser) {

        //Singleton Pattern so that only one driver is created
        if (null == driver) {
            switch (browser.toLowerCase()) {
                case "chrome":
                    driverManager = new ChromeDriverManager();
                    driver = driverManager.getDriverInstance();
                    break;

                case "firefox":
                    driverManager = new FirefoxDriverManager();
                    driver = driverManager.getDriverInstance();
                    break;
            }
        }

        return driver;
    }
}

  public void quitDriver() {
        if (null != driver) {
            driver.quit();
            //Initializing driver to null as in Singleton Pattern we want only one driver to be created
            driver = null;
        }
    }


Note: Its always a good practice to set driver to null in the last Step so that when you are quiting  the driver we should always satisfy the condition of Singleton class and set the driver value to null.



No comments:

Post a Comment