Refactoring 018 – Replace Singleton

Refactorings (21 Part Series)

1 Refactoring 001 – Remove Setters
2 Refactoring 002 – Extract Method
17 more parts…
3 Refactoring 003 – Extract Constant
4 Refactoring 004 – Remove Unhandled Exceptions
5 Refactoring 005 – Replace Comment with Function Name
6 Refactoring 006 – Rename Result Variables
7 Refactoring 007 – Extract Class
8 Refactoring 008 – Convert Variables to Constant
9 Refactoring 009 – Protect Public Attributes
10 Refactoring 010 – Extract Method Object
11 Refactoring 011 – Replace Comments with Tests
12 Refactoring 012 – Reify Associative Arrays
13 Refactoring 013 – Remove Repeated Code
14 Refactoring 014 – Remove IF
15 Refactoring 015 – Remove NULL
16 Refactoring 016 – Build With The Essence
17 Refactoring 017 – Convert Attributes to Sets
18 Refactoring 018 – Replace Singleton
19 Refactoring 019 – Reify Email Addresses
20 Refactoring 020 – Transform Static Functions
21 Refactoring 021 – Remove Dead Code

Breaking Free from the Evil Singleton

TL;DR: Refactor singletons to reduce coupling

Problems Addressed

Related Code Smells

图片[1]-Refactoring 018 - Replace Singleton - 拾光赋-拾光赋

Code Smell 32 – Singletons

Maxi Contieri ・ Nov 23 ’20

#codenewbie #tutorial #webdev #programming
图片[1]-Refactoring 018 - Replace Singleton - 拾光赋-拾光赋

Code Smell 22 – Helpers

Maxi Contieri ・ Nov 12 ’20

#oop #helpers #codenewbie #beginners
图片[1]-Refactoring 018 - 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(); 
    }
}

Enter fullscreen mode Exit fullscreen mode

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();

Enter fullscreen mode Exit fullscreen mode

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

图片[1]-Refactoring 018 - Replace Singleton - 拾光赋-拾光赋

Refactoring 007 – Extract Class

Maxi Contieri ・ Jul 4 ’22

#webdev #beginners #javascript #tutorial

See also

图片[1]-Refactoring 018 - Replace Singleton - 拾光赋-拾光赋

Singleton: The Root of all Evil

Maxi Contieri ・ Nov 17 ’20

#oop #tutorial #codenewbie #programming
图片[1]-Refactoring 018 - 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.

图片[1]-Refactoring 018 - Replace Singleton - 拾光赋-拾光赋

How to Improve your Code With easy Refactorings

Maxi Contieri ・ Oct 24 ’22

#webdev #beginners #programming #tutorial

Refactorings (21 Part Series)

1 Refactoring 001 – Remove Setters
2 Refactoring 002 – Extract Method
17 more parts…
3 Refactoring 003 – Extract Constant
4 Refactoring 004 – Remove Unhandled Exceptions
5 Refactoring 005 – Replace Comment with Function Name
6 Refactoring 006 – Rename Result Variables
7 Refactoring 007 – Extract Class
8 Refactoring 008 – Convert Variables to Constant
9 Refactoring 009 – Protect Public Attributes
10 Refactoring 010 – Extract Method Object
11 Refactoring 011 – Replace Comments with Tests
12 Refactoring 012 – Reify Associative Arrays
13 Refactoring 013 – Remove Repeated Code
14 Refactoring 014 – Remove IF
15 Refactoring 015 – Remove NULL
16 Refactoring 016 – Build With The Essence
17 Refactoring 017 – Convert Attributes to Sets
18 Refactoring 018 – Replace Singleton
19 Refactoring 019 – Reify Email Addresses
20 Refactoring 020 – Transform Static Functions
21 Refactoring 021 – Remove Dead Code

原文链接:Refactoring 018 – Replace Singleton

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容