Java面向对象设计问题:更新内部状态或返回新对象

发布于 2024-08-22 20:08:35 字数 1033 浏览 4 评论 0原文

这是一个设计问题。该设计是伪代码,代表一个小示例,但我将来可能会添加更多方法、数据、逻辑。

在此示例中,我正在考虑两种方法。在下面的执行方法中,我应该返回一个不可变的“数据/bean/模型”对象以及执行方法的输出,还是更新 BusinessLogic 类的状态。

两者都实现相同的目标,我想要执行的结果,并且数据应该包含在 bean 容器中或 BusinessLogic 类的内部。

我有点喜欢只拥有 BusinessLogic 类,因为 SomeObject 只是一个无用的 bean,不执行任何操作。

你有什么想法?

public class SomeObject  {
    private String data1;
    private String data2;
}

public class BusinessLogic {

    private final IWebObject webObject;

    /* String data1; String data2 */    

    public BusinessLogic(final IWebObject webObject) {
        this.webObject = webObject;
    }

    // Approach 1        
    public SomeObject execute() {
        return new SomeObject();
    }

    or
    ...
    ...

    // Approach 2
    public void execute() {
        // Do something
        this.data1 = "data1";
        this.data2 = "data2";
    }

    public String getData1() { }
    public String getData2() { } 

} // End of the Class //

我对方法 2 的唯一问题是 data1 和 data2 不会是不可变的。我可以任意调用执行并更改这些值。

This is a design question. The design is pseudo-code and represents a small example but I may add a lot more methods, data, logic in the future.

In this example, I am considering two approaches. In the execute method below, should I return an immutable "data/bean/model" object with the output of the execute method or update the state of the BusinessLogic class.

The both accomplish the same goal, I want the result of execute and either the data should be contained in a bean container or in the internally in the BusinessLogic class.

I am kind of favoring just having the BusinessLogic class because SomeObject is just a useless bean that doesn't do anything.

What are your thoughts?

public class SomeObject  {
    private String data1;
    private String data2;
}

public class BusinessLogic {

    private final IWebObject webObject;

    /* String data1; String data2 */    

    public BusinessLogic(final IWebObject webObject) {
        this.webObject = webObject;
    }

    // Approach 1        
    public SomeObject execute() {
        return new SomeObject();
    }

    or
    ...
    ...

    // Approach 2
    public void execute() {
        // Do something
        this.data1 = "data1";
        this.data2 = "data2";
    }

    public String getData1() { }
    public String getData2() { } 

} // End of the Class //

My only problem with approach 2 is that data1 and data2 won't be immutable so. I can call execute arbitrarily and change those values.

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

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

发布评论

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

评论(3

拥抱影子 2024-08-29 20:08:35

如果该方法的主要目的是执行并且不返回任何内容,我个人会采用第二种方法并将数据保留在BusinessLogic类内部。

If the main purpose of the method would be to execute and not return anything, I would personally go with your second approach and keep the data internally inside the BusinessLogic class.

嘿咻 2024-08-29 20:08:35

在大多数情况下,我认为这取决于调用execute() 方法的代码的性质。如果它只是按原样读取结果,那么将其保留在 BusinessLogic 类中就可以了。如果要将结果传递给不同的方法,则应该将结果放在单独的类中(您可以让 BusinessLogic 实现一个仅包含结果方法的接口,但这可能会模糊 BusinessLogic 实现之间的界限结束以及结果实现开始的地方)。

首先,最好将其保留在内部——遵循“你不会需要它”的原则。当您发现需要将结果传递给其他方法和其他对象时,您可以重构它以满足您当时的需求。

For the most part, I'd say it depends on the nature of the code that's calling the execute() method. If it's going to just be reading the results as-is, then keeping it in the BusinessLogic class would be fine. If you're going to be passing the results around to different methods, you should put the results in a separate class (you could have BusinessLogic implement an interface that contains just the result methods, but that could blur the line between where the BusinessLogic implementation ends and where the result implementation begins).

To start with, it'd probably be best to keep it internal -- following the "You Ain't Gonna Need It" principle. When the time comes where you can see that you are going to need to pass the results around to other methods and other objects, you can refactor it to suit your needs at the time.

空心↖ 2024-08-29 20:08:35

这不是一般的面向对象设计问题 - 它取决于您尝试建模的领域。

  • execute 是否应该更改 BusinessLogic 的状态? (选项 2)
  • 该类是如何被使用的? data1 和 data2 的使用方式是否不同?在不同的背景下? (选项 1)
  • data1 和 data2 的生命周期与 BusinessLogic 不同吗? (选项1)

This isn't a general object-oriented design question - it depends on the domain you're trying to model.

  • Should execute change the state of BusinessLogic or not? (Option 2)
  • How is that class being consumed? Are data1 and data2 used in a different way? In a different context? (Option 1)
  • Do data1 and data2 have different lifetimes than BusinessLogic? (Option 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文