Design Patterns Handbook
  • Introduction
  • Creational Patterns
    • Builder
    • Factory
    • Abstract Factory
    • Factory Method
    • Prototype
    • Singleton
    • Object Pool
    • Revealing Constructor
  • Structural Patterns
    • Adapter
    • Composite
    • Proxy
    • Flyweight
    • Facade
    • Bridge
    • Decorator
    • Private Class Data
  • Behavioral Patterns
    • Template Method
    • Mediator
    • Chain Of Responsibility
    • Observer
    • Strategy
    • Command
    • State
    • Visitor
    • Memento
    • Interpreter
    • Null Object
    • Iterator
    • Middleware
  • Clean Code Patterns
    • Extract Method
    • Clarify Responsibility
    • Remove Duplications
    • Keep Refactoring
    • Always Unit Test
    • Create Data Type
    • Comment to Better Name
    • Consistent Naming
    • If-else over ternary operator
    • Composition over Inheritance
    • Too Many Returns
    • Private to Interface
  • Anti Patterns
    • Big Ball of Mud
    • Singleton
    • Mad Scientist
    • Spaghetti Code
    • It Will Never Happen
    • Error Codes
    • Commented Code
    • Abbreviations
    • Prefixes
    • Over Patternized
    • Generic Interface over Function
Powered by GitBook
On this page

Was this helpful?

  1. Anti Patterns

Singleton

Singleton pattern is mentioned in both good patterns and also in anti patterns. Why? I think because singleton is easy to implement and many people wanted to use design patterns where ever it is possible, even when they didn't have a need to solve an issue. Then we ended up with code that is:

  • difficult to use

  • complex

  • difficult to test

Some people suggest that Singleton is an anti-pattern because it brings more evil than good. Still, Singleton pattern is useful when it is used correctly.

A lesson to learn here is "Use Singleton only when you face an issue, never just because you think it might make the code more cool or fancy".

Example of Singleton pattern

How to create an anti pattern of Singleton pattern? It should be easy. We just need to use Singleton pattern when we shouldn't use it.

public class UserFactory {

    private static UserFactory userFactory;

    private UserFactory() {
    }

    public static UserFactory getInstance() {
        if (userFactory == null) userFactory = new UserFactory();
        return userFactory;
    }

    public User createUser(String name) {
        return new User(name);
    }
}

class User {
    String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

class Main {
    public static void main(String [] args) {
        User john = UserFactory.getInstance().createUser("John");
        System.out.println(john.getName());
    }
}

There is no reason why a developer who is using this this code couldn't create an instance of User class by him self. The code above seems to be over-engineered.

PreviousBig Ball of MudNextMad Scientist

Last updated 5 years ago

Was this helpful?