Urgent.News

What's breaking now, across thousands of outlets.

Tech

Understanding the Singleton Pattern: From Core Concept to Thread-Safe Java

Imagine you are building an application with a settings screen. A user switches the app theme to "dark" , and ten different screens across the app need to apply that choice. If each screen instantiated its own settings object with new , one screen might read "dark" while another still holds "light" . What you actually need is one single settings object that is initialized once, exists as a single…

A common application needs a single settings object across all screens. The Singleton design pattern guarantees a class has only one instance and provides a global point of access to it. In Java, the classic lazy initialization implementation uses a private constructor, static getInstance() method, and cached instance. However, this basic version fails in multithreaded environments due to potential race conditions during instance creation.

Various approaches exist to fix the thread-safety issue, such as eager initialization, synchronized methods, double-checked locking, the Bill Pugh Holder idiom, and enums. Each method has trade-offs between thread safety, performance, and simplicity. While a Singleton offers convenience with shared global access, it also violates the Single Responsibility Principle, creates tight coupling, and can hinder unit testing.

Thus, before adopting a Singleton, the crucial question is whether the component truly requires a single unique instance or if it's chosen merely for convenience.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

TIL: How to Read GBase Database Package Names Before Installation

A package filename can tell you more than the database version. When deploying GBase Database(GBase 8a), it can also indicate the target CPU architecture, operating system family, and Python runtime.

  • Package filename reveals CPU architecture, OS family, Python runtime
  • Extract CPU architecture from filename segment (e.g., aarch64)
  • Verify OS identity and kernel using uname -m and uname -r commands

Detecting fake hi-res audio in the browser: a peak-hold FFT with the Web Audio API

The file header says 24-bit/96 kHz, but that only describes the container. If you want to know what is actually in the file, you have to look at the spectrum.

  • Detects fake high-res audio by analyzing spectrum, not file header
  • Prevents resampling by reading sample rate from file header first
  • Uses OfflineAudioContext for accurate decoding at correct sample rate

Shared household expenses without merging anyone's bank login

Roommates, couples, and small teams hit the same awkward wall: everyone needs a shared picture of “what we spent,” but nobody wants to hand a budgeting app their bank password — or merge three…

  • Money Tracker app helps roommates track expenses separately
  • Users create distinct ledgers for different purposes
  • App operates without linking bank accounts or sharing passwords

More from Monday 26 October →