</>CodexBash
All articles
Programming

Singleton and factory patterns

Two of the most used and most misused design patterns: what a singleton is, the thread-safe ways to write one and why it is often a smell, and the simple factory, factory method and abstract factory, with Java examples and when each earns its place.

CodexBash, , 5 min read

Design patterns are named solutions to problems that keep recurring in object-oriented code. Two of the first you meet are about creating objects: the singleton, which makes sure there is only one of something, and the factory, which decides which object to create so the caller does not have to. Both appear constantly in frameworks and interviews, and both are easy to overuse. This article covers how to write each correctly and when to reach for them.

Sign in to read the rest

The whole 5 minute article is free with an account, along with every problem, lesson and tutorial. No card.