🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)
🕵️ SicherheitslückenHak5: Hackers Just Poisoned the Rust Supply Chain | Threat Wire(01.09.2026 um 14:00 Uhr)
🕵️ SicherheitslückenHak5: Hackers Found a Way Into Humanoid Robots | Threat Wire(04.09.2026 um 15:04 Uhr)
🔧 AI Nachrichten Bits und so #1021 (Passwort für Laufwerk)(31.08.2026 um 22:15 Uhr)
🔧 AI Nachrichten Bits und so #1022 (Wie Weißbier)(06.09.2026 um 20:39 Uhr)
🍏 iOS / Mac OSHue-App 6.0 ist da: das sind die Neuerungen(07.09.2026 um 17:21 Uhr)

🔧 Programmierung 🕛 kürzlich 2 Min Lesezeit
0

Refactoring 018 - Replace Singleton

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Breaking Free from the Evil Singleton




TL;DR: Refactor singletons to reduce coupling







Problems Addressed




  • High

  • Multi-threading issues






Related Code Smells


















Steps




  1. Identify the singleton

  2. Locate all references to its getInstance() method

  3. Refactor the singleton to a standard class

  4. Inject it as a dependency






Sample Code






Before






CODE
public class DatabaseConnection {
private static DatabaseConnection instance;

private DatabaseConnection() {}

public static DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}

public void connect() {
}
}

public class Service {
public void performTask() {
DatabaseConnection connection = DatabaseConnection.getInstance();
connection.connect();
}
}







After





CODE
public class DatabaseConnection {  
// 1. Identify the singleton
public void connect() {
}
}

public class Service {
// 2. Locate all references to its getInstance() method.
private DatabaseConnection connection;

// 3. Refactor the singleton to a standard class.
public Service(DatabaseConnection connection) {
// 4. Inject it as a dependency.
this.connection = connection;
}

public void performTask() {
connection.connect();
}
}

DatabaseConnection connection = new DatabaseConnection();
// You can also mock the connection in your tests

Service service = new Service(connection);
service.performTask();







Type



[X] Semi-Automatic





Safety



This refactoring is safe when you update all references to the singleton and handle its dependencies correctly.



Testing each step ensures that no references to the singleton are missed.





Why the code is better?



Refactoring away from a singleton makes the code more modular, testable, and less prone to issues caused by the global state.



Injecting dependencies allows you to easily replace DatabaseConnection with a mock or different implementation in testing and other contexts.





Tags




  • Coupling





Related Refactorings











See also














Credits



Image by






This article is part of the Refactoring Series.




Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Hackers Just Poisoned the Rust Supply Chain | Threat Wire
1 Quelle
Hackers Found a Way Into Humanoid Robots | Threat Wire
1 Quelle
Bits und so #1021 (Passwort für Laufwerk)
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Refactoring 018 - Replace Singleton

Thematisch verwandte Begriffe: Refactoring, Replace, Singleton · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...