After reading What differentiates function objects from poltergeists?, according to the definition of poltergeist, I still don't understand why would "poltergeist" be a bad pattern:
A poltergeist is a short-lived object, typically stateless, which is used solely to trigger or initialize several other objects and then discarded. It is considered a consequence of poor object design
What I don't understand is, why can't solely to trigger or initialize several other objects be a good practice of encapsulation and reuse? I think I often do "solely to trigger or initialize several other objects", for example, creating a dialog and add it to main UI:
void addWelcomeDialog(View view,UserInfo userInfo){
Labal newNameLabel=new Label("Welcome, "+userInfo.nickname);
view.addSubview(newNameLabel);
.
.
.
Image genderImage=new Image("icon_"+userInfo.gender+".png");
.
.
.
}
new DialogHelper().addWelcomeDialog(this.glview,this.userInfo);
So addWelcomeDialog doesn't have its own state, and is just used to initialize bundles of UI objects. If it belongs to "poltergeist", was it required to delete "addWelcomeDialog", and copy and paste the codes in "addWelcomeDialog" to the place it originally calls "addWelcomeDialog"? Isn't it violating DRY principle?
And other question that mentions poltergeists : Creating an abstraction just for exception handling - a pattern or anti-pattern?
Useless classes with no real responsibility of their own, often used to just invoke methods in another class or add an unneeded layer of abstraction.
Why can't "just invoke methods in another class" be a real responsibility? For example, to draw something in a OpenGL view, the function may just invoke bundles of methods in a OpenGL view, eg:
void drawStatDiagram(GLView glview,StatData statData){
glview.setBackgroundColor(0.25,0.25,0.25);
glview.drawText("Graph of "+stateData.name,32,1,0,0,glview.width*0.5,glview.height*0.9);
.
.
.
//call a series of glview function to display stat graph
}
new OpenGLDrawer().draw(this.glview,this.stateData);
The way and the sequence of "invoke other object" is complex and hence encapsulated into a function in a separate helper class for reuse. So whats wrong with that approach?
So I think the definition of "poltergeists" is just "object that uses once only (or "new Object() that appears once only")" or "add an unneeded layer of abstraction". Is that true? If not, what am I misunderstood about "poltergeists"?
If it is complaining about "short-lived object", isn't the definition is just "the method can be static but currently isn't"?
If it is about "Don't put the things to encapsulate in object constructor" (eg:avoid new ObjectToDoSomething(this.data)), isn't put it into a separate static method (eg:ObjectToDoSometing.doSomething(this.data)) solves the problem? (Which implies that the root cause is not "solely to trigger or initialize several other objects", but place/way to "solely to trigger or initialize several other objects")
new Something(arg1).method(arg2)
suggest that you might wantSomething.method(arg1, arg2)
instead. In languages with free functions, there wouldn't even have to be a class involved. Of course objects still have their uses, like encapsulating state or providing polymorphism (in particular when implementing an interface).