在Java中的方法中在运行时初始化对象的最佳实践是什么?

发布于 2025-02-01 04:18:58 字数 957 浏览 2 评论 0原文

我有一个Java方法:

public void method(JSONObject jsonRequest, String token, int size) {
    DataRequest request = new DataRequest(size);
    request.from(jsonRequest);

    if (StringUtils.isNotEmpty(token)) {
        request.setPageToken(token);
    }

    request.doSomething();
}

我想对datareQuest of datareQuest of datareQuest sub-class 。做到这一点的一种方法是:

public void method(JSONObject jsonRequest, String token, int size) {
    DataRequest request = buildDataRequest(jsonRequest, token, size);
    request.from(jsonRequest);

    if (StringUtils.isNotEmpty(token)) {
        request.setPageToken(token);
    }

    request.doSomething();
}

protected DataRequest buildDataRequest(JSONObject jsonRequest, String token, int size) {
    return new DataRequest(size);
}

提取创建datareQuest对象的逻辑中的逻辑,然后让子类替代它。

有什么更好的实践吗?

I have a java method:

public void method(JSONObject jsonRequest, String token, int size) {
    DataRequest request = new DataRequest(size);
    request.from(jsonRequest);

    if (StringUtils.isNotEmpty(token)) {
        request.setPageToken(token);
    }

    request.doSomething();
}

I want to make it a runtime decision on whether to use DataRequest or sub-class of DataRequest. One way to do that is the following:

public void method(JSONObject jsonRequest, String token, int size) {
    DataRequest request = buildDataRequest(jsonRequest, token, size);
    request.from(jsonRequest);

    if (StringUtils.isNotEmpty(token)) {
        request.setPageToken(token);
    }

    request.doSomething();
}

protected DataRequest buildDataRequest(JSONObject jsonRequest, String token, int size) {
    return new DataRequest(size);
}

Extracting the logic of creating the DataRequest object into another method and let the sub-classes override it.

Is there any better practice to achieve this?

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

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

发布评论

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

评论(1

↘人皮目录ツ 2025-02-08 04:18:58

您可以创建一个功能接口创建类似的实例(由于参数列表,您无法使用供应商或(bi)功能)

@FunctionalInterface
interface DataRequestFactory {
    DataRequest buildDataRequest(JSONObject jsonRequest, String token, int size);
}

:提供两个构造函数(也许默认工厂不是必要的):

public MyClass() {
    this((r, t, s) -> new DataRequest(s));
}

public MyClass(DataRequestFactory factory) {
    this.factory = factory;
}

现在您可以调用this.factory.builddatarequest(jsonobject jsonrequest,string token,string token,int size size>)

You can create a functional interface creating the instance like (due to your parameter list you cannot use Supplier or (Bi)Function):

@FunctionalInterface
interface DataRequestFactory {
    DataRequest buildDataRequest(JSONObject jsonRequest, String token, int size);
}

Your class provides two constructors (maybe the default factory is not neccessary):

public MyClass() {
    this((r, t, s) -> new DataRequest(s));
}

public MyClass(DataRequestFactory factory) {
    this.factory = factory;
}

Now you can call this.factory.buildDataRequest(JSONObject jsonRequest, String token, int size).

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