In
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, 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
An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
. The original definition is by Michael Akroyd at the 1996 Object World West Conference:
A poltergeist can often be identified by its name; they are often called "manager_", "controller_", "supervisor", "start_process", etc.
Sometimes, poltergeist classes are created because the
programmer
A programmer, computer programmer or coder is an author of computer source code someone with skill in computer programming.
The professional titles Software development, ''software developer'' and Software engineering, ''software engineer' ...
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
In object-oriented programming, the command pattern is a Behavioral pattern, behavioral Design pattern (computer science), design pattern in which an object is used to Information hiding, encapsulate all information needed to perform an action or ...
, 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
A pattern is a regularity in the world, in human-made design, or in abstract ideas. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeated l ...
such as
model–view–controller
Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divides the related program logic into three interconnected elements. These elements are:
* the model, the internal representat ...
, 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
Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Offi ...
or as a
mixin.
There have been proposed methods in detecting poltergeists in code for refactoring.
See also
*
Anti-pattern
An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
*
Factory (object-oriented programming)
In object-oriented programming, a factory is an object for creating other objects; formally, it is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be ''new''. More broadly, ...
*
YAGNI principle
References
*
External links
Development AntiPatterns
Anti-patterns
{{compu-prog-stub