advantages of page factory in selenium code example

Example 1: advantages of page factory in selenium

/*** 
* Constructor 
* @param driver an instance of WebDriver 
*/
public int TimeoutValue = 30;
public SearchResultsPage(Webdriver driver) { 
    PageFactory.initElements(new AjaxElementLocatorFactory(driver, TimeoutValue), this);
}

Example 2: advantages of page factory in selenium

public class BasePage {

private By username = By.id("username");
private By password = By.id("password");
private By loginBtn = By.name("loginbtn");

  public void userLogin(String userName, String password) {
        driver.findElement(username).sendKeys("testuser");
        driver.findElement(password).sendKeys("testpassword");
        driver.findElement(loginBtn).click();
  }
}

Example 3: advantages of page factory in selenium

public class BasePage {
  @FindBy(id= "username") private WebElement userName;
  @FindBy(id= "password") private WebElement password;
  @FindBy(id= "login") private WebElement loginBtn;

  public void userLogin(String userName, String password) {
    userName.sendKeys(userName);
    password.sendKeys(password);
    loginBtn.click();
  }
}