Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以应用哪些模式来避免方法重载此类中的所有方法?_Java_Design Patterns - Fatal编程技术网

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的功能

什么样的模式最适合处理这样的问题?我计划完全重写这个课程,但我想创造一个好的立体设计

我的想法:

  • 策略模式(窗口切换策略、框架切换策略、警报切换策略)
  • 某种生成器,当我们只创建一个方法时,我们可以建立状态来决定是否应该将持续时间传递给新的Wait();构造函数与否
  • 伪窗口(int索引){}:

    希望在这里有一个可选的持续时间,如果是这样的话,我们可以改为这样做,但是当我们不需要调用方指定的显式持续时间时,可以提供很好的重用性:

    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索引或字符串键)确定
    类型