Member-only story

3 Java Patterns that died in 2022

Greg L. Turnquist
6 min readOct 6, 2022

--

If there’s something we LOVE when it comes to software development, it’s finding ourselves some fun little shortcuts.

Of course, we don’t call them that. We call them patterns. Patterns are like recipes for baking up software solutions. Solutions we see appear more than once. After all, if we’ve solved a problem, why not re-use the solution? Isn’t that what Object-Oriented Programming (OOP) is all about?

However, some of these patterns we solved for long ago, have turned out to be HORRIBLE and the source of misery. And so we’re going to see some of the most vile patterns that died a deserved death in 2022 starting with…

1 — Static Global Objects a.k.a. Singleton Pattern

This is that beast of a pattern where we use trickery and hacks to ensure that No More Than One of these things could exist. Often, they involved external resources, for which there can be only one!

Now what is bad about these things?

Well, the trickery used to implement them often involved statics and taking advantage of when static initialization happens. HOT TIP: It’s early in an application’s lifecycle when static initialization happens.

Static objects have certain side effects that make them truly detestable including:

  • Hard to test
  • Not overrideable
  • VERY visible

Static, global objects violate some key principles. Being global, it’s hard to override. That’s because EVERYONE could be using it. To override it could break some component YOUR component isn’t even aware of.

And because they are static (again, to ensure their lifecycle is fixed and under control across the lifetime of the application), automated tests have to deal with that.

Because they are so hard to mutate, the inevitably adopt another strategy. It was not uncommon to migrate a single-value static global into a map, to afford multi-valued support. And once you start making it possible for everyone to stuff anything they want into it, the thing basically turns into a magical Bag of Holding!

--

--

Greg L. Turnquist
Greg L. Turnquist

Written by Greg L. Turnquist

Sr. Staff Technical Content Engineer at CockroachDB • YouTube Content Creator at https://youtube.com/@ProCoderIO • Best-Selling Author • Coffee Lover

Responses (10)

Write a response