The Singleton Pattern - Anti-Pattern or Solution? - Klaus Iglberger - C++ on Sea 2022

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • C++ on Sea Website: cpponsea.uk/
    C++ on Sea Twitter: / cpponsea
    ---
    The Singleton Pattern - Anti-Pattern or Solution? - Klaus Iglberger - C++ on Sea 2022
    Slides: github.com/phi...
    Singleton is indeed a rather infamous pattern: There are many voices out there that describe Singleton as a general problem in code, as an anti-pattern, as dangerous, or even as evil. Still, despite the bad reputation and the many negative aspects, if used judiciously and if implemented properly,
    Singleton has proven to be a useful solution for representing the few global aspects in your code.
    This talk tries to give a balanced overview of the Singleton pattern. I will show the cons as well as the pros and will demonstrate how to properly deal with Singletons to overcome the major disadvantages. In particular, I will show how to implement Singletons such that dependencies to implementation details are avoided, code can be easily changed and full testability is given.
    ---
    Klaus Iglberger
    Klaus Iglberger is a freelancing C++ trainer and consultant. He has finished his PhD in computer science in 2010 and since then is focused on large-scale C++ software design. He shares his experience in popular advanced C++ courses around the world (mainly in Germany, but also the EU and US). Additionally, he is the initiator and lead designer of the Blaze C++ math library (bitbucket.org/...) and a (co-)organizer of the Munich C++ user group (www.meetup.com....
    ---
    C++ on Sea is an annual C++ and coding conference, in Folkestone, in the UK. The 2022 conference was conducted as a two part, physical and online C++ event.
    Annual C++ on Sea, C++ conference: cpponsea.uk/
    2022 Program: cpponsea.uk/20...
    ---
    Produced and Edited by Digital Medium Ltd: events.digital...
    Enquiries: events@digital-medium.co.uk
    #Programming​ #Cpp​ #CppOnSea​

КОМЕНТАРІ • 4

  • @user-kf8yp9vl9p
    @user-kf8yp9vl9p Рік тому +1

    Definitely something to use carefully, but sometimes the best solution. The example Klaus gives is what I commonly refer to as a "Supper Simple Singleton" -- in this case it simply provides a pointer to the implementing object. Another Supper Simple Singleton that I have used many times is a registry for a service. Objects that want to use the service register and then the service calls them. Note that this is for embedded systems that never stop or change registration after initialization. Thanks!

  • @Roibarkan
    @Roibarkan Рік тому

    Very thought provoking talk, Thanks Klaus. Another c++ use case which might relate to the topic is the “predicate” member of std::map or the “hasher” of std::unordered_map. There too, the STL uses the ‘strategy’ pattern, yet specific implementations/instantiations might require (implementation detail?) only a single object used. To some extent p1169 adds the notion of such singletons to c++23 (classes with static operator(), that might themselves have static variables)

  • @Roibarkan
    @Roibarkan Рік тому

    12:12 (slide 22) - as an “implementation pattern” I think one of the merits of hiding globals as static variables in functions is that they handle initialization order fairly well

  • @sanderjobing
    @sanderjobing 2 роки тому +1

    Anti-pattern: Can introduce global state and does not comply to the Liskov substitution principle. So difficult testable.