13

Someone recently asked a question about the Ghost Design Pattern - I have not seen this before.

What is the Ghost Design Pattern and how is it implemented? I can only find snippets on the web in reference to it.

3
  • 1
    During architecture interviews I used to ask people, "so, can you tell me anything about the LeftHandedSpinWidget design pattern?" and watch them wrestle with the confusion. There is no LeftHandedSpinWidget design pattern. Jun 9, 2010 at 18:05
  • @Swingline That's cruel. Ghost is a genuine pattern though. Jun 9, 2010 at 18:07
  • Really sad to talk about a pattern that does not exists, I mean, almost everyone agrees that the "Ghost pattern" refers to a Proxy or Lazy Loading but are not 100% sure. GoF said : The pattern's name conveys the essence of the pattern succinctly. A good name is vital, because it will become part of your design vocabulary.
    – Marcote
    Jun 9, 2010 at 18:16

4 Answers 4

23

The only reference I've ever heard to a Design Pattern and 'Ghost' is in Lazy-Loading.

Since Lazy-loading involves only loading the object when it's actually needed, you can think of it as a 'Ghost' until then. You can see its outline, but can't really use it until it's loaded.

0
5

Ghosts are mentioned in PoEAA, pp 202, 206-14. A ghost is a lazy loaded object that contains just enough info to instantiate itself on demand. They can be useful because they can trigger a bulk load of similar ghosts on the first access if they register themselves with a loader (dunno if Fowler mentions that bit though).

4

It's not in GOF nor Fowler PoEAA, the only thing I can think of it as something similar its a Proxy for lazy loading.

0
0

I actually just created one and then realized it was a Ghost pattern after asking a question here in SO. Follow that link for the original PHP code, here's a pseudo-code version:

// A "cheap" class
class Namespace_Original
  {
  // The expensive, uninitialised object
  private Original

  // The "cheap" data needed to initialize Original
  private Data

  // Constructor for the cheap class
  public Namespace_Original(Data)
    {
    this.Data = Data
    }

  // Whenever you call a method of Original
  public __call(method_name, method_data)
    {
    // Create the expensive object only the first time it's going to be used
    if (empty(this.Original))
      this.Original = new Original(this.Data);

    // Call the Original's method with it's arguments
    this.Original.name( method_data );
    }
  }

When you initialize this cheap class, nothing is created inside it. It's when you actually call a method that the "Original" object gets created, if there was none, and data retrieved from that. It makes you not to initialize Original if you're not going to use it, providing it's an expensive operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.