Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 31, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 31, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 31, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 31, 2025

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025

      How to install SteamOS on ROG Ally and Legion Go Windows gaming handhelds

      May 31, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025
      Recent

      Oracle Fusion new Product Management Landing Page and AI (25B)

      May 31, 2025

      Filament Is Now Running Natively on Mobile

      May 31, 2025

      How Remix is shaking things up

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025
      Recent

      Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

      May 31, 2025

      Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

      May 31, 2025

      I love Elden Ring Nightreign’s weirdest boss — he bargains with you, heals you, and throws tantrums if you ruin his meditation

      May 31, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: Single or Mingle

    CodeSOD: Single or Mingle

    April 9, 2025
    CodeSOD: Single or Mingle

    Singletons is arguably the easiest to understand design pattern, and thus, one of the most frequently implemented design patterns, even- especially– when it isn’t necessary. Its simplicity is its weakness.

    Bartłomiej inherited some code which implemented this pattern many, many times. None of them worked quite correctly, and all of them tried to create a singleton a different way.

    For example, this one:

    public class SystemMemorySettings
    {
        private static SystemMemorySettings _instance;
    
        public SystemMemorySettings()
        {
            if (_instance == null)
            {
                _instance = this;
            }
        }
    
        public static SystemMemorySettings GetInstance()
        {
            return _instance;
        }
    
        public void DoSomething()
        {
        ...
            // (this must only be done for singleton instance - not for working copy)
            if (this != _instance)
            {
                return;
            }
        ...
        }
    }
    

    The only thing they got correct was the static method which returns an instance, but everything else is wrong. They construct the instance in the constructor, meaning this isn’t actually a singleton, since you can construct it multiple times. You just can’t use it.

    And you can’t use it because of the real “magic” here: DoSomething, which checks if the currently active instance is also the originally constructed instance. If it isn’t, this function just fails silently and does nothing.

    A common critique of singletons is that they’re simply “global variables with extra steps,” but this doesn’t even succeed at that- it’s just a failure, top to bottom.

    [Advertisement]
    Keep the plebs out of prod. Restrict NuGet feed privileges with ProGet. Learn more.

    Source: Read More 

    Hostinger
    Facebook Twitter Reddit Email Copy Link
    Previous Article14 Best Free and Open Source Electronic Design Automation Tools
    Next Article Personalizziamo un po’ GNOME – Versione 2025

    Related Posts

    News & Updates

    Windows 11 version 25H2: Everything you need to know about Microsoft’s next OS release

    May 31, 2025
    News & Updates

    Elden Ring Nightreign already has a duos Seamless Co-op mod from the creator of the beloved original, and it’ll be “expanded on in the future”

    May 31, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Get 23% OFF the ‘SteelSeries Arctis Nova Pro Wireless’ headset for Xbox / PC — arguably the best high-end multi-device headset you can get

    News & Updates

    How to Import Assets in Godot [FREE]

    Development

    How JavaScript’s at() method makes array indexing easier

    Development

    Input Leap – KVM software

    Linux

    Highlights

    Data breach at Total Fitness exposed almost half a million people’s photos – no password required

    June 18, 2024

    UK-based gym chain Total Fitness has been accused of sloppy security, following the discovery of…

    Overview of.NET MAUI: Easily Developing Cross-Platform Applications

    January 8, 2025

    Playing with Light and Refraction in Three.js: Warping 3D Text Inside a Glass Torus

    March 13, 2025

    NVIDIA AI Releases cuPyNumeric: A Drop-in Replacement Library for NumPy Bringing Distributed and Accelerated Computing for Python

    November 29, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.