Choosing a Creational Pattern

Choosing a creational design pattern: telling Factory Method, Abstract Factory, Builder, Prototype and Singleton apart, and when a plain constructor is enough.

  • 5 cards
  • 4 questions
  • About 9 min
  • Intermediate to Advanced
Share

Five creational patterns can each be made to fit almost any problem that creates an object, which is why choosing between them is a skill of its own. The choice turns on one question: what varies when the object is created? The class, a whole family of classes, the parts of one product, the object to start from, or nothing at all.

This topic opens on the three patterns most often confused, Factory Method, Abstract Factory and Builder, with one document export problem solved all three ways so that the difference is the only thing that changes. The second comparison adds Prototype and Singleton, and ends a decision flowchart at the plain constructor, the answer when nothing varies.

The gotcha is a factory added where a constructor was enough, and the exercise gives six problems that are not the earlier topics' examples, each to be matched with a pattern or with the constructor.

By the end you can choose a creational pattern for a stated problem, give the reason, and recognise when the right choice is none of them.

Contents

5 cardsnot started