P
P
Paul2016-10-29 23:02:41
C++ / C#
Paul, 2016-10-29 23:02:41

Is it possible to implement the "Singleton" design pattern using a non-private constructor?

Interested in the possibility of implementing this pattern in C # without a private constructor
---------------------------------------- -------------------------------------------------- ----------------
Expanded the question.
Is it possible to create a condition inside the constructor that would prohibit the creation of objects
Like this:

class Singleton
{
    private int countInstance;
    public Singleton()
    {
        lock (syncRoot) {
        if (!countInstance == 0) 
        { // код, запрещающий создание экземпляра 
        }
        else 
        {
            countInstanse = 1;
        }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2016-10-30
@yarosroman

No, the constructor is made private so that only the class itself has access to the constructor. Otherwise, anyone can create a singleton.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question