Retiring the Singleton Pattern, Concrete Suggestions for What to Use Instead - Pete Muldoon ACCU 21

Поділитися
Вставка
  • Опубліковано 22 вер 2024
  • #Programming #Cpp #AccuConf
    Slides: accu.org/conf-...
    ACCU Website: www.accu.org
    ACCU Conference Website: conference.acc...
    ACCU Twitter: @ACCUConf
    Streamed & Edited By Digital Medium Ltd: events.digital...
    ------
    “The worst part of this whole topic is that the people who hate singletons rarely give concrete suggestions for what to use instead.”
    In this talk, we will explore just such an approach for replacing the Singleton pattern in large codebases.
    It is easy to slip into the pattern of creating singletons - particularly in large legacy code bases - where low level functions need to propagate side effects like database updates, IPC etc.
    Passing parameters down long function call chains can be daunting in terms of scope of change required in a large codebase.
    Additionally, users calling a long established API in legacy code are frequently unwilling to change their calls to supplement the current data being passed in.
    After briefly reviewing a classic singleton approach to a typical problem - sending requests to a server - and it associated drawbacks.
    We will rework the example and replace the function's internal singleton calls with calls to an explicitly passed in wrapper class. The extra information required for legacy users is injected via a custom default instance of the wrapper so that the users of the original function require no coding changes.
    We will then show how the previously untestable function can now be subjected to unit testing via dependency injection.
    This idea is later expanded to cover
    * keeping ABI stable
    * dealing with non-copyable types
    * dealing with delayed construction
    * dealing with Singleton dependency groupings
    * Initialization order of interdependent singletons, replacing error prone explicit intialization ordering with hard to misuse automatic initialization driven by the language.
    * Showing how the replacement pattern can be gradually introduced to a large code base instead of 'all at once'.
    This alternative approach has been successfully employed in multiple areas in Bloomberg where developers believed there was no other feasible choice.
    ------
    Pete Muldoon
    Pete Muldoon has been using C++ since 1991. Pete has worked in Ireland, England and the USA and is currently employed by Bloomberg. A consultant for over 20 years prior to joining Bloomberg, Peter has worked on a broad range of projects and code bases in a large number of companies both tech and finance. Such broad exposure has, over time, shown what works and what doesn't for large scale engineering projects. He's a proponent of elegant solutions and expressive code.
    ------
    Future Conferences:
    ACCU 2022 Spring Conference, Bristol (UK), Marriott City Centre:
    2022-04-05 to 2022-04-09.
    -------------------------

КОМЕНТАРІ • 4

  • @petermuldoon8111
    @petermuldoon8111 3 роки тому +1

    An update of a talk given previously at Cppcon 2021 with 2 new sections : Injecting configuration, Injecting lifetimes

  • @markofilipovic9260
    @markofilipovic9260 3 роки тому +1

    Yet Another Singleton Pattern talk? Seriously? I wonder when will people stop giving so much attention to this antipattern, and let it slip into oblivion...

    • @petermuldoon8111
      @petermuldoon8111 3 роки тому +3

      reality is many systems have them , this is a talk about getting rid of them

    • @superscatboy
      @superscatboy Рік тому +1

      The talk is about *retiring* the singleton pattern - in other words it's about a practical solution to making the singleton pattern "slip into oblivion".
      As explained in the introduction of the talk, it's all well and good saying "singletons bad, don't use", but without good suggestions of what to replace them with the statement is basically useless and they'll continue to appear.