In computer programming, a poltergeist (or gypsy wagon) is a short-lived, typically stateless object used to perform initialization or to invoke methods in another, more permanent class. It is considered an anti-pattern. The original definition is by Michael Akroyd at the 1996 Object World West Conference:
A poltergeist can often be identified by its name; they often include words such as "", "", "", "", etc. in the name.
Sometimes, poltergeist classes are created because the programmer anticipated the need for a more complex architecture. For example, a poltergeist arises if the same method acts as both the client and invoker in a command pattern, and the programmer anticipates separating the two phases. However, this more complex architecture may actually never materialize.
Poltergeists should not be confused with long-lived, state-bearing objects of a pattern such as modelâÂÂviewâÂÂcontroller, or tier-separating patterns such as business delegate pattern.
To remove a poltergeist, delete the class and insert its functionality in the invoked class, possibly by inheritance or as a mixin.
There have been proposed methods in detecting poltergeists in code for refactoring.
This <code>Poltergeist</code> class in this C++ example can be seen as a "poltergeist object", due to not adding additional functionality or encapsulation and only increasing complexity with unnecessary abstraction.
This could instead be more appropriately done using a smart pointer.
Another example of a poltergeist/gypsy wagon object, is the following, where <code>UserCreator</code> is instantiated just to perform some basic actions.
This could be more appropriately done like so, avoiding any poltergeist class entirely: