简体   繁体   中英

implement singleton design pattern and use it throughout classes

I have a console app where I solved one algorithm in it. I had all the logic in program.cs file, but then I was asked to implement Object Oriented design, and use separation of concerne more. So I started separating my logic in different classes, but in program.cs I had 4 global variables that I would use in my logic to solve the algorithm.

Now I have separated these 4 global variables and I've put them in a single class. What I need is to use these fields, in the same way as I had in my program.cs I need to use these 4 global variables throughout the classes and methods, where I assign different values to them.

I have tried to implement Singleton design pattern, as I thought this would suit my case, because I would instantiate the class only once, and I would store these values to use them in different moments, with their specific values.

So basically I would need one class to be instantiated only once, and to be used throughout other classes but also to preserve the values as I would with global variables.

This is my code

class cars.cs

public void ForbidenConfig(int number)
{
    Collections c = new Collections();
    Wheels3 w3 = Wheels3.GetInstance; // here I get the instance of the class

    int k = 1;
    Configurations[] block = new Configurations[number];
    for (int i = number; i > 0; i--)
    {
        Console.Write("Test case " + k + ": ");
        //bla bla bla another logic here
        

        //below I want to add data to collections
        w3.Collections.Add(block[k - 1]);
        k++;
    }
    c.AddCollections(object1, object2);
}

Now I have also this other class collections.cs where I have this code

public void AddCollections(CarsConfigurations config, CarsConfigurations previousConfig)
{
     Cars w3 = Cars.GetInstance; // here I get the instance of the class
    if (CheckConfiguration(config) == false)
    {
        config.SetPreviousState(previousConfig);

        //below I also add data to another field of the singleton class
        w3.AddCollections2.Add(config);
    }
}

now this is my singletton class

public void AddCollections(CarsConfigurations config, CarsConfigurations previousConfig)
{
     Cars w3 = Cars.GetInstance; // here I get the instance of the class
    if (CheckConfiguration(config) == false)
    {
        config.SetPreviousState(previousConfig);
        w3.AddCollections.Add(config);
    }
}


 public sealed class Cars
{
    private static List<CarsConfigurations> ConfigCollections = new List<CarsConfigurations>();
    private static List<CarsConfigurations> ClosedStateCollections = new List<CarsConfigurations>();
   
    
    private static Cars instance = null;
    public static Cars GetInstance
    {
        get
        {
            if (instance == null)
                instance = new Cars();
            return instance;
        }
    }

    private Cars()
    {
        List<CarsConfigurations> ConfigCollections = new List<CarsConfigurations>();
        List<CarsConfigurations> ClosedStateCollections = new List<CarsConfigurations>();
    }
    public static void ConfigCollection()
    {
        return this.ConfigCollections; // this line gives me an error ofcourse,  Member 'Cars.ConfigCollections' cannot be accessed with an instance reference; qualify it with a type name instead 

    } 
    
}

Can I achieve what I'm looking for with Singleton design pattern..? If yes how can I manage it? Is there any other way? Can you guys please help me, I have this task to deliver, and I'm really out of time? Can you give any other ideas?

I would really really appreciate some help. Thank you in advance!

I was asked to implement Object Oriented design

In that case, a glorified set of global variables doesn't address that concern. The Singleton design pattern has its uses, but not like this .

Instead, as a start, pass the variables around as method arguments:

public void AddCollections(
    CarsConfigurations config,
    CarsConfigurations previousConfig,
    Cars w3)
{
    if (CheckConfiguration(config) == false)
    {
        config.SetPreviousState(previousConfig);

        w3.AddCollections2.Add(config);
    }
}

Once you've done that, you may want to consider if further modelling is possible. For example, the above method has both Cars and two CarsConfiguration objects. Would it make sense to somehow combine those two classes?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM