Let's understand that This articles is about Factory Pattern with selenium, donot get confused with Page factory pattern.
Define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate
Providing simple choice to the user and letting the backend people to handle the difficulties, In barbershop example, we just want to get the haircut but we donot cut our hairs. Cutting the hair is the problem of barbar.
Based on the given input, we have to provide the output object from list of sub classes
Below are few things that you should know while directing / suggesting a barbarshop
First you will create a Interface which contains a abstract class called haircut, as you are aware that all the classes which implement the interface should provide implementation for all the abstract methods present in the Interface. (this is eulaent to criteria of haircut in a shop)
Now we have to create sub classes for the Interface ( these sub classes are nothing but barbar shops), So we have to provide implementation to the abstract methods.
Now we have to create a class which does not inherit the Interface but decides which object to invoke. It is nothing but you who does not have barbar shop but know all the barbar shops.
The object in the classes will be returned based one criteria / value, like which gender is asking.
Create a interface called HairCut_Criteria, this interface has a abstract method called haircut() .
interface HairCut_Criteria{
boolean haircut();
}
Whoever implements the HairCut_Criteria, they have to provide implementation to the haircut().
ForMale and ForFemale both classes has implemented the haircut() because they are sub-classes of HairCut_Criteria Interface.
class ForMale implements HairCut_Criteria{
@Override
public boolean haircut() {
System.out.println("Hair cut is finished for man & price is INR 120");
return true;
}
}
class ForFemale implements HairCut_Criteria{
@Override
public boolean haircut() {
System.out.println("Hair cut is finished for Woman & price is INR 700");
return true;
}
}
Create class which represents your position (who know better barber shops), For this I have created a class called You.
You class will decide barber shop or return the object of required class based on a decision like Man or Woman.
class You {
public static HairCut_Criteria suggestAShop(String criteria) {
if(criteria.equals("man")) {
return new ForMale();
}else {
return new ForFemale();
}
}
}
It is time for class which represents your friend, who will ask you which barbershop is better.
public class TheFriend{
public static void main(String[] args) {
// suggest for man
You.suggestAShop("man").haircut();
// suggest for woman
You.suggestAShop("woman").haircut();
}
}
Complete code for factory Pattern
interface HairCut_Criteria{
boolean haircut();
}
class ForMale implements HairCut_Criteria{
@Override
public boolean haircut() {
System.out.println("Hair cut is finished for man & price is INR 120");
return true;
}
}
class ForFemale implements HairCut_Criteria{
@Override
public boolean haircut() {
System.out.println("Hair cut is finished for Woman & price is INR 700");
return true;
}
}
class You {
public static HairCut_Criteria suggestAShop(String criteria) {
if(criteria.equals("man")) {
return new ForMale();
}else {
return new ForFemale();
}
}
}
public class TheFriend{
public static void main(String[] args) {
// suggest for man
You.suggestAShop("man").haircut();
// suggest for woman
You.suggestAShop("woman").haircut();
}
}
So far we have seen what is factory pattern, lets see how pattern will be used with selenium
Yes, I wanted to explain it simple way that why i got to write above one. Now let's see how can we implements same thing with Selenium.
Open different browser with selenium
Complete program for Factory Pattern with webdriver.
class DriverDecider {
public static WebDriver suggestAShop(String criteria) {
if(criteria.equals("chrome")) {
return new ChromeDriver();
}else if(criteria.equals("firefox")) {
return new FirefoxDriver();
}else if(criteria.equals("ie")) {
return new InternetExplorerDriver();
}
// if none of the above browser matches
return null;
}
}
public class TheFriend{
public static void main(String[] args) {
// create chrome driver
DriverDecider.suggestAShop("chrome");
}
}
I can hear you guys are screming like Why donot you use Switch case, that is right.
It is true that when we have more than one if else block then we should try to use the switch cases. So let me rewite the above example using swicth cases
class DriverDecider {
public static WebDriver getDriver(String criteria) {
switch (criteria) {
case "chrome":
return new ChromeDriver();
case "firefox":
return new FirefoxDriver();
case "ie":
return new InternetExplorerDriver();
default:
// if none of the above browser matches
return null;
}
}
}
public class TheFriend{
public static void main(String[] args) {
// create chrome driver
WebDriver driver = DriverDecider.getDriver("chrome");
driver.get("https://chercher.tech");
}
}
Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know.
Knowledge is the only thing that doubles when you spend it.
I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation