在java中正确使用接口

发布于 2025-01-09 12:15:27 字数 878 浏览 3 评论 0原文

具有此接口

public interface BookService {

List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet);
}

当在其实现中不使用变量 int childLimitPages 时,具有 2 种实现。

 public class AdultBookList implements BookService {

 // This won't work since there is missing variable, I am asking if there is other way to provide this ask
     List<Book> search(int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
       .....
     }
 }

 public class ChildrenBookList implements BookService {

     List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
       .....
     }
 }

我的问题我应该如何使用它?我应该有2个接口吗?我可以有默认变量吗?

Having this interface

public interface BookService {

List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet);
}

Having 2 implementations when one does not use variable int childLimitPages in its implementation.

 public class AdultBookList implements BookService {

 // This won't work since there is missing variable, I am asking if there is other way to provide this ask
     List<Book> search(int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
       .....
     }
 }

 public class ChildrenBookList implements BookService {

     List<Book> search(int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet) {
       .....
     }
 }

My question how should I use it? Should I have 2 interface? Could I have default variable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凯凯我们等你回来 2025-01-16 12:15:27

当您实现接口时,您需要定义具有与接口中声明的完全相同的签名的所有方法。

因此该类

    public class AdultBookList implements BookService {

         List<Book> search(int numberOfBooks, 
                           List<Book> sourceOneResultSet, 
                           List<Book> sourceTwoResultSet) {
       .....
     }
 }

不是接口 BookService 的有效实现,因为方法 search 具有不同的签名。

您可以通过不同的方式解决它。

第一种方法是重载接口中的方法搜索,并提供不带 childLimitPages 参数的方法。在实现中,如果您使用 childLimitPages 调用该方法,则可以在 AdultBookList 中引发异常。但我建议你用第二种更优雅的方式来解决它。

第二种方法是将所有参数替换为包装它们的对象。在此对象中,您可以定义默认值,因此当未显式设置该值时,它将采用默认值,如下所示:

public interface BookService {
    List<Book> search(SearchRequest request);
}

...
public class SearchRequest {
    // I replaced int with Integer to hold also null values
    private Integer childLimitPages;

    // Defining a default value
    private int numberOfBooks = 10;
    private List<Book> sourceOneResultSet;
    private List<Book> sourceTwoResultSet;

    ...
}

When you implement an interface you need to define all methods with exactly the same signature declared in the interface.

So the class

    public class AdultBookList implements BookService {

         List<Book> search(int numberOfBooks, 
                           List<Book> sourceOneResultSet, 
                           List<Book> sourceTwoResultSet) {
       .....
     }
 }

is not a valid implementation of the interface BookService because the method search has a different signature.

You can solve it in different ways.

First way is to overload the method search in the interface and provide also the method without the childLimitPages parameter. In the implementations you can throw an exception in the AdultBookList if you are calling the method with the childLimitPages. But I suggest you a more elegant way to solve it in the second way.

Second way is to replace all the parameters with an object wrapping them. In this object you can define default values, so when it is not explicitly set the value it takes the default value, so something like:

public interface BookService {
    List<Book> search(SearchRequest request);
}

...
public class SearchRequest {
    // I replaced int with Integer to hold also null values
    private Integer childLimitPages;

    // Defining a default value
    private int numberOfBooks = 10;
    private List<Book> sourceOneResultSet;
    private List<Book> sourceTwoResultSet;

    ...
}
若能看破又如何 2025-01-16 12:15:27

您可以将第一个参数定义为 Optional 并断言在 AdultBookService 中存在Optional 中的值。

public interface BookService {
    List<Book> search(Optional<Integer> limit, int numberOfBooks, List<Book> sourceA, List<Book> sourceB);
}

public class AdultBookList implements BookService {

    List<Book> search(Optional<Integer> limit, int numberOfBooks, List<Book> sourceA, List<Book> sourceB) {
    }
}

public class ChildrenBookList implements BookService {

    List<Book> search(Optional<Integer> childLimitPages, int numberOfBooks, List<Book> sourceA, List<Book> sourceB) {
    }
}

我喜欢我的 API 在末尾有可选参数。并将调用重定向到解析选项的内部计算。就像这样:

public class AdultBookList implements BookService {

    List<Book> search(int numberOfBooks, List<Book> sourceA, List<Book> sourceB, Optional<Integer> limit) {
        if (limit.isEmpty())
        {
            System.err.printf("The limit should have been set!");
            return Collections.emptyList();
        }
        return internalSearch(numberOfBooks, sourceA, sourceB, limit.get());
    }

    private List<Book> search(int numberOfBooks, List<Book> sourceA, List<Book> sourceB, int limit) {
        
    }
}

You could define the first argument as Optional<Integer> and assert that in the AdultBookService the value in the Optional is present.

public interface BookService {
    List<Book> search(Optional<Integer> limit, int numberOfBooks, List<Book> sourceA, List<Book> sourceB);
}

public class AdultBookList implements BookService {

    List<Book> search(Optional<Integer> limit, int numberOfBooks, List<Book> sourceA, List<Book> sourceB) {
    }
}

public class ChildrenBookList implements BookService {

    List<Book> search(Optional<Integer> childLimitPages, int numberOfBooks, List<Book> sourceA, List<Book> sourceB) {
    }
}

I like my APIs to have optional arguments at the end. And to redirect the calls to an internal computation where the optionals are resolved. Like so:

public class AdultBookList implements BookService {

    List<Book> search(int numberOfBooks, List<Book> sourceA, List<Book> sourceB, Optional<Integer> limit) {
        if (limit.isEmpty())
        {
            System.err.printf("The limit should have been set!");
            return Collections.emptyList();
        }
        return internalSearch(numberOfBooks, sourceA, sourceB, limit.get());
    }

    private List<Book> search(int numberOfBooks, List<Book> sourceA, List<Book> sourceB, int limit) {
        
    }
}
玉环 2025-01-16 12:15:27

在您的界面上定义多个方法。

您有两种不同的行为,因此您必须提供两种不同的方法。提供 searchForPageCountsearchByNumberOfBooks,而不是一种 search 方法。

public interface BookService {

    List<Book> searchForPageCount ( int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet ) ;

    List<Book> searchByNumberOfBooks ( int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet ) ;

}

Define multiple methods on your interface.

You have two different behaviors, so you must offer two different methods. Instead of one search method, offer searchForPageCount and searchByNumberOfBooks.

public interface BookService {

    List<Book> searchForPageCount ( int childLimitPages, int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet ) ;

    List<Book> searchByNumberOfBooks ( int numberOfBooks, List<Book> sourceOneResultSet, List<Book> sourceTwoResultSet ) ;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文