Home  >  Article  >  Java  >  Refactoring - Replace Singleton

Refactoring - Replace Singleton

Barbara Streisand
Barbara StreisandOriginal
2024-11-26 09:23:09950browse

Breaking Free from the Evil Singleton

TL;DR: Refactor singletons to reduce coupling

Problems Addressed

  • High coupling
  • Difficult testability
  • Multi-threading issues

Related Code Smells

Refactoring  - Replace Singleton

Code Smell 32 - Singletons

Maxi Contieri ・ Nov 23 '20

#codenewbie #tutorial #webdev #programming
Refactoring  - Replace Singleton

Code Smell 22 - Helpers

Maxi Contieri ・ Nov 12 '20

#oop #helpers #codenewbie #beginners
Refactoring  - Replace Singleton

Code Smell 25 - Pattern Abusers

Maxi Contieri ・ Nov 15 '20

#oop #tutorial #programming #patterns

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

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

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

Refactoring  - Replace Singleton

Refactoring 007 - Extract Class

Maxi Contieri ・ Jul 4 '22

#webdev #beginners #javascript #tutorial

See also

Refactoring  - Replace Singleton

Singleton: The Root of all Evil

Maxi Contieri ・ Nov 17 '20

#oop #tutorial #codenewbie #programming
Refactoring  - Replace Singleton

Coupling: The one and only software design problem

Maxi Contieri ・ Feb 6 '21

#webdev #programming #oop #tutorial

Credits

Image by PublicDomainPictures from Pixabay


This article is part of the Refactoring Series.

Refactoring  - Replace Singleton

How to Improve your Code With easy Refactorings

Maxi Contieri ・ Oct 24 '22

#webdev #beginners #programming #tutorial

The above is the detailed content of Refactoring - Replace Singleton. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn