Method Chaining (Builder Pattern)
While writing codes , ever wondered how Java codes (Selenium) or vbs (QTP) allows user todo something like : Object.method().method() .... and so on .
Eg :
Browser("Jobs").Page("QTP").WebElement(" ") //In QTP
Webdriver.FindElement("xxxx").sendkeys("123) ; //In Selenium
The above style uses a pattern called Builder pattern internally.
Example :
public class test1 {public static void main(String[] args){
animal a =new animal();
a.set_name("dog").bites(false);
}
}
public class animal {
String animal_name;
boolean Bites;
public animal set_name(String animal_name){
this.animal_name=animal_name;
return this;
}
public animal bites(boolean Bites){
this.Bites=Bites;
return this;
}
}
Thing to notice :
1. Each method uses "this" keyword while returning
2. Each method returns an object of the class type.
End Result :
a.set_name("dog").bites(false);
No comments:
Post a Comment