Note that there are some explanatory texts on larger screens.

plurals
  1. POWho is responsible for setting the object properties?
    text
    copied!<p>I have an object with fields. In a project I need this object filled with information.</p> <p>whose responsibility is to fill this object?</p> <p>I would like a method like getFilledObject() that returns a full filled object to my program.</p> <p>I can put this logic into the main flow, and fill it "in place", also I can add this method into the class itself, so that it would know how to fill itself according to the parameters I pass. The latter decision is good enough, the only problem is that the class must then know much more about it's nature than a simple plain get-set class.</p> <p>But I feel neither decision is good enough. What should it be? A helper class that contains only the logic to fill the object?</p> <p><hr> You know, yes, Factory is what I was looking for, but it is always mentioned along with polymorphism. In my case I don't need it at all. So I can call it a factory, but would it really be what they called "A Factory pattern"?</p> <p>So what do we have as a result? first, static factory method</p> <pre> class A { ObjType1 obj1; ObjType2 obj2; public A() {} public static getA(int param1, int param2) { A a = null; ObjType3 obj3 = null; if (somelib.complexCheck(param)) { obj3 = someotherlib.getSomething(param2); // other stuff } if (obj3 != null) { // do something with getters & setters, fill a } return a; } </pre> <p>and Factory pattern or something similar to it</p> <pre> class Fact { public static A getA(int param1, int param2) { // do all that stuff here } } </pre> <p>the difference in my opinion would be lots of import directives in the A class in case of <i>static factory method </i>. Also, we can put similar static methods into the Fact class if all those need the same libraries.</p> <p>What would you choose between the two?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload