Code Monkey home page Code Monkey logo

fsc.simplesingleton's Introduction

FSC.SimpleSingleton

SimpleSingleton is a thread-safe singleton pattern implementation in C# that allows registration and retrieval of singleton instances of any type. It ensures that only one instance of a particular type can be created and accessed at a time, providing a globally accessible point of access for these instances.

Explanations:

Gets the singleton instance of a type, if it exists.

  • SimpleSingleton.InstanceOf<T>() : T (Instance)

Determines whether a singleton instance of a type exists.

  • SimpleSingleton.HasInstanceOf<T>() : bool

Registers a singleton instance of a type.

  • SimpleSingleton.RegisterInstance<T>(T instance) : bool

Unregisters a singleton instance of a type.

  • SimpleSingleton.UnregisterInstanceOf<T>() : bool

Returns a list of all instances registered in the SimpleSingleton.

  • SimpleSingleton.GetAllInstances() : List<object>

Code example:

using System.Text;

using FSC.Singleton;

namespace Test
{
    internal static class Program
    {
        static void Main(string[] args)
        {
            // Create instances of StringBuilder and TestClass
            StringBuilder sbInstance = new StringBuilder();
            TestClass tcInstance = new TestClass();

            // Register the instances
            bool sbRegistered = SimpleSingleton.RegisterInstance(sbInstance);
            bool tcRegistered = SimpleSingleton.RegisterInstance(tcInstance);

            // Add the text "Hello World" to the StringBuilder instance
            StringBuilder? sb = SimpleSingleton.InstanceOf<StringBuilder>();
            bool sbAppended = sb?.Append("Hello World ") != null;

            // Get the content of the StringBuilder and the number of TestClass
            string sbContent = sb?.ToString() ?? string.Empty;
            int tcNumber = SimpleSingleton.InstanceOf<TestClass>()?.Number ?? 0;

            // Output the content and number
            string output = $"{sbContent}{tcNumber}";
            Console.WriteLine(output);

            // Unregister the instances
            bool sbUnregistered = SimpleSingleton.UnregisterInstanceOf<StringBuilder>();
            bool tcUnregistered = SimpleSingleton.UnregisterInstanceOf<TestClass>();

            // Output success or failure messages
            Console.WriteLine(sbRegistered && tcRegistered ? "Instances successfully registered" : "Error registering instances");
            Console.WriteLine(sbAppended ? "Text successfully appended" : "Error appending text");
            Console.WriteLine(sbUnregistered && tcUnregistered ? "Instances successfully unregistered" : "Error unregistering instances");
        }
    }

    internal class TestClass
    {
        internal int Number => 24;
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.