<SomeThirdPartyClass>Util==神对象?
<SomeThirdPartyClass>Util == God object?
我用Selenium Webdriver a lot, and there are a lot of "utility" methods I've written to make it easier for me to use. I put these classes in a WebDriverUtil
class, and now that file is over 1,200 lines long. Every method in WebDriverUtil
attempts to separate me from using WebDriver
because it's something I use a lot that wouldn't be DRY继续写作。
例如,这是我将放入 WebDriverUtil
的方法。
public void waitUntilVisible(final WebElement webElement) {
new WebDriverWait(webDriver, 10).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver webDriver) {
return webElement.isDisplayed();
}
});
}
如果我有 1,200 行充满这样方法的代码,我有 God object 吗?如果是这样,我该如何解决?
我应该像这样将我的行为分离到装饰器 类 中吗?
public class WebElementDecorator implements WebElement {
private WebElement webElement;
private final WebDriver webDriver;
public WebElementDecorator(WebElement webElement, WebDriver webDriver) {
this.webElement = webElement;
this.webDriver = webDriver;
}
public void waitUntilVisible() {
new WebDriverWait(webDriver, 10).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver webDriver) {
return webElement.isDisplayed();
}
});
}
public void click() {
webElement.click();
}
//... other WebElement methods
}
If I have 1,200 lines of code full of methods like this, do I have a God object?
单凭代码行数并不能充分说明class是否神似。由于糟糕的编码风格、过度工程、过度专业化方法的不同变体、冗长的语言、内联注释等,class 可能会因代码而变得臃肿。
神 class 是一个被责任臃肿的神。这里有两个试金石来判断你的utilclass是否已经进化成神class:
更改测试时对实用程序的影响 class。如果对测试子集的更改导致您经常更改和重新编译您的 util class,那么这可能表明您的 util class 服务于太多的主人。理想情况是,对测试子集的更改只会影响那些与测试直接相关的 util 方法(如果需要)。
更改 util class 时对测试 class 的影响。如果更改您的 util class 的一部分导致许多 不相关的 测试出现意外失败,那么您的 util class 可能已经将它的触角伸向了您的所有测试。
If so, how could I fix it? Should I separate my behavior into decorator classes like this?
最好从小的增量步骤开始重构。使用您的代码示例,我将首先使用 isVisible()
、isEnabled()
等方法将所有 Wait..Until..Predicate
代码简单地提取到名为 WaitUntilEvent()
或其他名称的单独 class 中, isSelected()
等。示例用法如下所示:
WaitUntilEvent waitUntil = new WaitUntilEvent(webElement, webDriver);
waitUntil.isVisible();
webElement.click();
// etc..
如果我需要围绕 Wait..Until..Predicate
更改我的测试要求(例如超时间隔),我知道只有一个 class 可以编辑。然后可以将其进一步重构为 until(PredicateIsTrue).then(PerformAction)
、until(FunctionIsTrue).then(PerformAction)
等。我更喜欢这种方法,而不是包罗万象的神 class WebElementDecorator
,后者可能会以许多装饰方法捕获许多不同行为。
我用Selenium Webdriver a lot, and there are a lot of "utility" methods I've written to make it easier for me to use. I put these classes in a WebDriverUtil
class, and now that file is over 1,200 lines long. Every method in WebDriverUtil
attempts to separate me from using WebDriver
because it's something I use a lot that wouldn't be DRY继续写作。
例如,这是我将放入 WebDriverUtil
的方法。
public void waitUntilVisible(final WebElement webElement) {
new WebDriverWait(webDriver, 10).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver webDriver) {
return webElement.isDisplayed();
}
});
}
如果我有 1,200 行充满这样方法的代码,我有 God object 吗?如果是这样,我该如何解决?
我应该像这样将我的行为分离到装饰器 类 中吗?
public class WebElementDecorator implements WebElement {
private WebElement webElement;
private final WebDriver webDriver;
public WebElementDecorator(WebElement webElement, WebDriver webDriver) {
this.webElement = webElement;
this.webDriver = webDriver;
}
public void waitUntilVisible() {
new WebDriverWait(webDriver, 10).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver webDriver) {
return webElement.isDisplayed();
}
});
}
public void click() {
webElement.click();
}
//... other WebElement methods
}
If I have 1,200 lines of code full of methods like this, do I have a God object?
单凭代码行数并不能充分说明class是否神似。由于糟糕的编码风格、过度工程、过度专业化方法的不同变体、冗长的语言、内联注释等,class 可能会因代码而变得臃肿。
神 class 是一个被责任臃肿的神。这里有两个试金石来判断你的utilclass是否已经进化成神class:
更改测试时对实用程序的影响 class。如果对测试子集的更改导致您经常更改和重新编译您的 util class,那么这可能表明您的 util class 服务于太多的主人。理想情况是,对测试子集的更改只会影响那些与测试直接相关的 util 方法(如果需要)。
更改 util class 时对测试 class 的影响。如果更改您的 util class 的一部分导致许多 不相关的 测试出现意外失败,那么您的 util class 可能已经将它的触角伸向了您的所有测试。
If so, how could I fix it? Should I separate my behavior into decorator classes like this?
最好从小的增量步骤开始重构。使用您的代码示例,我将首先使用 isVisible()
、isEnabled()
等方法将所有 Wait..Until..Predicate
代码简单地提取到名为 WaitUntilEvent()
或其他名称的单独 class 中, isSelected()
等。示例用法如下所示:
WaitUntilEvent waitUntil = new WaitUntilEvent(webElement, webDriver);
waitUntil.isVisible();
webElement.click();
// etc..
如果我需要围绕 Wait..Until..Predicate
更改我的测试要求(例如超时间隔),我知道只有一个 class 可以编辑。然后可以将其进一步重构为 until(PredicateIsTrue).then(PerformAction)
、until(FunctionIsTrue).then(PerformAction)
等。我更喜欢这种方法,而不是包罗万象的神 class WebElementDecorator
,后者可能会以许多装饰方法捕获许多不同行为。