How can I instantiate a singleton just to implement it?

The fact that I ask this question and do not see it anywhere makes me believe that my design / thinking is wrong, so it is very good, and my implementation can be greatly improved.

I have a singleton class ( like here ) in which I create material for a bunch of test classes. I use singleton, so my test objects should not be re-built in every testing class. Now there are some fields, such as those BaseEconomyassigned inside singleton, that I need in each test class. Thus, each test class has an initialization method similar to this:

[TestInitialize]
public void Initialize()
        {
            Singleton instance = Singleton.Instance
            _baseDate = Singleton.BaseDate;
            _baseEconomy = Singleton.BaseEconomy;
            // etc. etc.
        }

The string is Singleton instance = Singleton.Instancecompletely necessary for the implementation of my singleton, however I do not consider it fundamentally correct, because I never use a variable instance.

So, how do I instantiate a singleton for the sole purpose of having an instance for assigning and accessing fields for each test class? Do I really need to assign it to a variable that I will never use?

+4
source share
1 answer

BaseDate BaseEconomy Singleton, singleton. singleton, - Instance ( Instance) singleton ( , - ). , singleton:

[TestInitialize]
public void Initialize()
{
    _baseDate = Singleton.BaseDate;
    _baseEconomy = Singleton.BaseEconomy;
    // etc. etc.
}

. , singleton, BaseDate BaseEconomy, :

[TestInitialize]
public void Initialize()
{
    _instance = Singleton.Instance;
}

[Test]
public void Foo()
{
   // use _instance.BaseDate 
   // use _instance.BaseEconomy
}
+3

Source: https://habr.com/ru/post/1626834/


All Articles