6

I'm trying to understand what is Poltergeist antipattern, and how does it differ from Command or Delegate patterns. I've read:

http://en.wikipedia.org/wiki/Poltergeist_(computer_science) http://sourcemaking.com/antipatterns/poltergeists

But didn't understand the difference...

So to make it clear I would like to see the code example of it (I prefer C# or Java languages).

Does anybody have it?

Wug
  • 12,956
  • 4
  • 34
  • 54
Oleg Oshkoderov
  • 500
  • 1
  • 4
  • 17
  • I am currently reading Anti Patterns book, where this antipattern is described. I think there's something wrong with the explanation if one cannot understand it, and I can't. I don't see the problem with short lived objects, in fact, "Replace method with method object" refactoring does just that: it creates a class whose instances are short lived. But they have state. Maybe the problem with poltergeists is that they are stateless (e. g., some utility class that has non-static methods)? But still there are use cases to use (almost) stateless classes to achieve polymorphic behavior. – Vytenis Bivainis Jul 29 '15 at 21:05
  • I noticed one class in our codebase and was wondering if it might be a poltergeist. It's name is ImageFromBmp. It's constructor takes $url as sole parameter and there is one useful method that returns images instance. – Josef Sábl Aug 28 '17 at 15:45
  • Is there a way that this function could be moved into the image's class? If so, then you may have a poltergeist. – James A Mohler Aug 28 '17 at 22:57

2 Answers2

3

Wikipedia describes the command pattern with:

The command pattern is a behavioural design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. One component can send a command to another specific component, with the assumption that when some condition is met, the command will be fired.

This concept is very like that of a functor in functional programming (A functor is basically a function in a black box, arguments and all)

The poltergeist is described with:

The poltergeist is a short-lived, typically stateless object used to perform initialization or to invoke methods in another class.

Commands are generic and have to be able to contain enough state to be reused. Poltergeists are usually special purpose and exist only to rattle some chairs and make loud noises in the basement, then to disappear. Poltergeists are usually used as a crutch to help construct or initialize an object and are rarely used to share changes in state after construction.

In other words, yes, they are vaguely similar, but poltergeists are inflexible and represent a static action, and commands are generic, can be reusable, and represent a configurable action.

Wug
  • 12,956
  • 4
  • 34
  • 54
  • I'm trying to imagine the case when some class were created just to 'help construct or initialize an object'. Is it something like a stub for some dependencies or like a factory, that creates object initialized in particular way? – Oleg Oshkoderov Oct 09 '12 at 14:54
  • Thats the thing, there is no conceivable need for one. They usually arise because of programmer misconceptions and there is [almost] never a real reason to have one. – Wug Oct 09 '12 at 14:57
  • 1
    How is this different from having a Controller class in an MVC architecture? – Alexandr Mar 30 '16 at 07:14
0

Value Objects, by definition tend to fall into this category.

http://en.wikipedia.org/wiki/Value_object

They exist to push values around, but don't do anything.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
  • Care to explain this a bit more? I don't think this is true at all. So I guess I am missing something :-) – Josef Sábl Aug 28 '17 at 15:27
  • 1
    "Value Object don't do anything" - it's a very strong statement :). Validation, formatting, convertion, DSL extension, specific data operations, and more. Take a lot to the joda.Money class, for example of good and functional value object. – Stepan Mozyra Mar 27 '21 at 21:10