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:[citation needed]

As a gypsy wagon or a poltergeist appears and disappears mysteriously, so does this short lived object. As a consequence the code is more difficult to maintain and there is unnecessary resource waste. The typical cause for this anti-pattern is poor object design.

A poltergeist can often be identified by its name; they often include words such as "Manager", "Controller", "Supervisor", "StartProcess", 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.[1]

Example

edit

This Poltergeist 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.

import std;

using String = std::string;

// Poltergeist class that just holds a pointer, but adds no meaningful behavior
class Poltergeist {
private:
    String* s; // pointer to string, but the class itself doesn't do anything useful
public:
    explicit Poltergeist(String* s):
        s{s} {}

    ~Poltergeist() {
        delete s;
    }

    [[nodiscard]]
    String get() const noexcept {
        return s;
    }

    // No additional behavior or meaningful functionality
};

int main() {
    // Create a Poltergeist object that just holds a pointer to the string
    Poltergeist p(new String("Hello, world!"));

    // Just passes the data around without adding value
    std::println(*p.get());

    return 0;
}

This could instead be more appropriately done using a smart pointer.

import std;

using String = std::string;
template <typename T>
using UniquePtr = std::unique_ptr<T>;

// Use smart pointers directly to manage memory
UniquePtr<String> s = std::make_unique<String>("Hello, World!");
std::println(*s);

Another example of a poltergeist/gypsy wagon object, is the following, where UserCreator is instantiated just to perform some basic actions.

import std;

using String = std::string;

class UserManager {
public:
    void createUser(const String& name) {
        std::println("User created: {}", name);
    }
};

// The poltergeist class
class UserCreator {
public:
    explicit UserCreator(const String& name) {
        UserManager manager;
        manager.createUser(name);
    }
};

int main() {
    // Creating a poltergeist just to call createUser()
    UserCreator("Alice");
    UserCreator("Bob");
}

This could be more appropriately done like so, avoiding any poltergeist class entirely:

// Avoid the UserCreator poltergeist entirely
int main() {
    UserManager manager;
    manager.createUser("Alice");
    manager.createUser("Bob");
}

See also

edit

References

edit
  1. ^ Al-Rubaye, Samer Raad Azzawi; Selcuk, Yunus Emre (24–26 November 2017). "An investigation of code cycles and Poltergeist anti-pattern". 2017 8th IEEE International Conference on Software Engineering and Service Science (ICSESS). pp. 139–140. doi:10.1109/ICSESS.2017.8342882. ISBN 978-1-5386-0497-7.
edit


📚 Artikel Terkait di Wikipedia

Poltergeist (disambiguation)

(born 1971), Brazilian model, performance artist and actress Poltergeist (computer programming), short-lived, typically stateless object used to perform

List of software anti-patterns

anti-patterns. Some relate to software design, object-oriented programming, computer programming, methodological, configuration management, DevOps, and other

Anti-pattern

pattern – Deceptive user interface designs Design smell – Term in computer programming ISO/IEC 29110: Software Life Cycle Profiles and Guidelines for Very

List of TRS-80 Color Computer games

the Dragon 32, a very similar computer. There are three main models: Color Computer 1, Color Computer 2, and Color Computer 3. Some games are only compatible

List of Central Washington University people

Of Fame. Craig T. Nelson — star of sitcom Coach and actor in the film Poltergeist Brian Thompson — known for his work in action films and television series

Tron

Extra-Terrestrial, Star Trek II: The Wrath of Khan, Blade Runner and Poltergeist. The soundtrack for Tron was written by pioneer electronic musician Wendy

Good Boy (2025 Leonberg film)

directorial debut of Leonberg, who conceived the idea after re-watching Poltergeist (1982) and noting the horror trope of dogs sensing the presence of ghosts

Inheritance (disambiguation)

from the Airheads soundtrack "Inheritance", by John van Tongeren from Poltergeist: The Legacy "Inheritance", by Katatonia from Night Is the New Day "The