Java 我可以应用哪些模式来避免方法重载此类中的所有方法?
目前,我正在研究重构一个类,并一直在研究设计模式,我很好奇哪种模式适用于这个场景。当前类的外观:Java 我可以应用哪些模式来避免方法重载此类中的所有方法?,java,design-patterns,Java,Design Patterns,目前,我正在研究重构一个类,并一直在研究设计模式,我很好奇哪种模式适用于这个场景。当前类的外观: public WebDriver window(int index) {//stuff}; public WebDriver window(String nameOrId) {//stuff}; public WebDriver windowByTitle(String title) {//title specific stuff}; public WebDriver frame(int inde
public WebDriver window(int index) {//stuff};
public WebDriver window(String nameOrId) {//stuff};
public WebDriver windowByTitle(String title) {//title specific stuff};
public WebDriver frame(int index) {//stuff};
public WebDriver frame(String nameOrId) {//stuff};
public WebDriver frameByTitle(String title) {//title specific stuff};
public WebDriver alert(int index) {//stuff};
public WebDriver alert(String nameOrId) {//stuff};
public WebDriver alertByTitle(String title) {//title specific stuff};
现在让我们假设这9个方法中的每一个,我想添加一个可选的次要参数,即持续时间
,例如:
public WebDriver window(int index, Duration of) {//stuff}
但我需要这些方法中的每一个都有这样的功能,我不想创建9个重载方法,重用大量的核心代码。这些方法中的每一个都会创建一个新的Wait
,它调用一个标准构造函数来创建它,假设默认的Duration
,但我想提供提供自己的Duration的功能
什么样的模式最适合处理这样的问题?我计划完全重写这个课程,但我想创造一个好的立体设计
我的想法:
new Wait(duration).until(windowIsReady());
等等,看起来像这样:
public Wait() {}
public Wait(Duration duration) {}
public static class Select {
final Type type;
// Only one of these should be set.
int index;
String key;
private Select(Type type) {
this.type = type;
}
private Select setIndex(int index) {
this.index = index;
return this;
}
private Select setKey(String key) {
this.key = key;
return this;
}
public static Select by(Type type, int index) {
assert (type == Index);
return new Select(type).setIndex(index);
}
public static Select by(Type type, String key) {
assert (type != Index);
return new Select(type).setKey(key);
}
enum Type {
Index,
NameOrId,
Title;
}
}
// Now only one of each.
public WebDriver window(Select select) {
}
public WebDriver frame(Select select) {
}
public WebDriver alert(Select select) {
}
private void test() {
// Easy to use.
window(Select.by(Index, 1));
window(Select.by(NameOrId, "hello"));
}
谢谢我认为问题已经在代码中,应该先解决。如果您使用这样的方法:
public Wait() {}
public Wait(Duration duration) {}
public static class Select {
final Type type;
// Only one of these should be set.
int index;
String key;
private Select(Type type) {
this.type = type;
}
private Select setIndex(int index) {
this.index = index;
return this;
}
private Select setKey(String key) {
this.key = key;
return this;
}
public static Select by(Type type, int index) {
assert (type == Index);
return new Select(type).setIndex(index);
}
public static Select by(Type type, String key) {
assert (type != Index);
return new Select(type).setKey(key);
}
enum Type {
Index,
NameOrId,
Title;
}
}
// Now only one of each.
public WebDriver window(Select select) {
}
public WebDriver frame(Select select) {
}
public WebDriver alert(Select select) {
}
private void test() {
// Easy to use.
window(Select.by(Index, 1));
window(Select.by(NameOrId, "hello"));
}
您现在可以非常简单地添加新参数,因为现在每种类型只有一个方法,不需要添加更多来添加其他参数。所有方法都返回
WebDriver
,正在创建它们,或者它们已经存在并被找到。将duration作为参数添加到当前方法中并允许其为null?@joakimDanielson如果不是向后兼容的,将破坏太多的客户端代码。我需要保持这个类面向公众的接口不变,我可以改为使用Duration。。。varargs?将类型作为参数传递(并具有断言)不是多余的吗。我们可以通过其他参数类型(int索引或字符串键)确定类型