lundi 19 août 2019

Does a static class considered a singleton? [duplicate]

This question already has an answer here:

I was asked to implement a singleton in my code and I was curious if, for example, had a static class with all of the methods public static without implementing a c'tor. is that considered a Singelton or I actually need to create a single instance?

    public static class TenBestFriendsAlgorithm
    {
        private static bool s_WasAlgorithmActivated = false;

        public static bool WasAlgorithmActivated
        {
            get { return s_WasAlgorithmActivated; }
        }

        public static List<UserRating> BestFriendsAlgorithm(User i_LoggedInUser)
        {
            //Doesn't matter
            s_WasAlgorithmActivated = true;
            //Doesn't matter
        }
    }




When I use the class as followed, I also use double-check lock for thread safety.

        private void tenBestFriendsAlgorithm()
        {
            if (!TenBestFriendsAlgorithm.WasAlgorithmActivated)
            {
                lock (r_TenBestFriendsAlgorithmContext)
                {
                    if (!TenBestFriendsAlgorithm.WasAlgorithmActivated)
                    {
                        updatePicturesInTenBestFriendsTab(TenBestFriendsAlgorithm.BestFriendsAlgorithm(m_LoggedInUser));
                    }
                }
            }
        }

I'd like to know if this is considered a Singelton.

Aucun commentaire:

Enregistrer un commentaire