20
flexzuu
8y

Learning to use Singletons in Java. Then reading online that it might be an antipattern. Software Engineering ... 👌

Comments
  • 1
    Useful to know how they work and their purpose and how they can work with other patterns like factory. Scala does them natively with 'object' and makes them easy to use.
  • 0
    Welcome to DevRant!
  • 0
    @flexzuu
    Definitely an antipattern. Learn why they're bad 😊
  • 2
    It's not an anti pattern if you are using it correctly and not overusing it.
  • 0
    Imho it's very useful sometimes
  • 0
    @kaqqao
    In your option are all single instance objects singletons?
  • 2
    @kaqqao
    The singleton pattern dictates that it provides global access to itself, what you're describing (and what IoC containers do) is not this.

    People do not like the singleton pattern. It hides dependencies and makes testing far more difficult.

    Having only one instance of an object in your application isn't a bad thing, I agree with you on that.
  • 1
    like all things in programming, like factories, regexs, multiple inheritence, and singeltons: depends how you use them.
    Overuse of a singelton, coupled with one or more of the mistakes above is a disaster for future maintenance of the codebase. Always make sure your usecase makes sense.
  • 0
    @kaqqao
    Yeah, the spring docs are great. They explain the difference between their singletons and the GoF singletons far better than I was about to try to!
Add Comment