java - 如何优化参数
问题描述
我必须在我的方法中传递这么多参数,所以请建议我如何优化这些参数。
如何优化这些参数,使我的代码看起来干净高效
// 下面是代码。
public class ContactPage extends BasePage {
WebDriver driver;
@FindBy(xpath = "//*[text()='Contact Information']")
WebElement contactPageHeader;
@FindBy(xpath = "//*[@id='contactForm']/table/tbody/tr[2]/td[1]/table/tbody/tr[1]/td[2]/select")
WebElement title;
@FindBy(xpath = "//input[@id = 'first_name']")
WebElement firstName;
@FindBy(xpath = "//input[@id = 'middle_initial']")
WebElement middleName;
@FindBy(xpath = "//*[@id = 'surname']")
WebElement lastName;
@FindBy(xpath = "//input[@type='text' and @name='nickname']")
WebElement nickName;
@FindBy(xpath = "//input[@name='client_lookup']")
WebElement company;
@FindBy(xpath = "//input[@id='company_position']")
WebElement position;
@FindBy(xpath = "//input[@id='department']")
WebElement department;
@FindBy(xpath = "//input[@name='contact_lookup_sup']")
WebElement supervisor;
@FindBy(xpath = "//input[@type='text' and @name='contact_lookup_ref']")
WebElement referredBy;
@FindBy(xpath = "//input[@id='mobile']")
WebElement mobile;
@FindBy(xpath = "//input[@id='email']")
WebElement email;
@FindBy(xpath = "//input[@id='im_id']")
WebElement messengerId;
@FindBy(xpath = "//input[@id='skype_id']")
WebElement skypeId;
@FindBy(xpath = "//input[@type='text' and @name='identifier']")
WebElement identifier;
@FindBy(xpath = "//input[@type='text' and @name='address_title']")
WebElement addressTitle;
@FindBy(xpath = "//input[@id= 'tags']")
WebElement tags;
@FindBy(xpath = "//*[@id='contactForm']/table/tbody/tr[1]/td/input[2]")
WebElement save;
public ContactPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public boolean verifyContactPageHeader() {
WebDriverWait wait = new WebDriverWait(driver, Constants.DEFAULT_WAIT_TIMEOUT);
wait.until(ExpectedConditions.visibilityOf(contactPageHeader));
return contactPageHeader.isDisplayed();
}
public void createNewContact(String FirstName, String MiddleName, String LastName, String Nickname, String Company,
String Position, String Department, String Supervisor, String ReferredBy, String Mobile, String Email,
String MessengerID, String SkypeID, String Identifier, String AddressTitle, String tags,
String Description) {
}
仅在上面的块中为我的给定代码寻求代码优化。
解决方案
- 创建一个 Contact 类,将您传递给 createNewContact() 的所有值作为字段,并为每个字段创建 getter 方法
- 将构建器模式应用于该类,如此 处所示
- 重构 createNewContact() 以将 Contact 对象作为参数,并使用 getter 方法从 Contact 对象中提取值
推荐阅读
- python - 当pandas中有很多条件时如何过滤数据?
- dagger-2 - 匕首中的配置文件
- node.js - 日期未在 MongoDB 中更新
- identityserver4 - 您将收到的令牌存储在 OpenID-connect 客户端的什么位置?
- javascript - 为什么在 Material-UI 文档中使用 useMemo 来进行自动完成?
- asynchronous - 在需要特征对象的上下文中使用借用参数的异步函数
- git - 在不执行 git rebase 的情况下更改提交的父级
- c - C 检查器中的密码似乎不起作用
- python - 使用 hdfql 将多个 CSV 文件导入 hdf5?
- elasticsearch - 有没有办法在现有数据上启用 _source?