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.