mercredi 13 juillet 2016

Which design pattern could I use to create many instances of a single class?

I have been looking for any answers, but all the other questions dont match my problem...

I have a single class called Element it has 3 properties, name, ID and type

now...i got another class called CreateData...in which i use a for loop to call a function... in that function i send some parameters....but non of them is of type Element...(but i can add it if that can help...see the question at the bottom)

then in that function i create an instance of type Element, and use it...

so...the for loop can go over a 100000 times...and i always have to create a new element with new parameters...in every loop...and with that its very slow...not only because of that, but it is one of my problems that i can solve...

here my question:

is there a pattern that can help me speed up my program?

Here is my code... NOTE: these are two classes, one is the Element and the other is CreateData

public class Element
{
    public string ID { get; set; } 
    public string Type { get; set; }    
    public string Name { get; set;}     
}

CreateData()
...
foreach (var file in files)
        {
            int numberOfErrors = 0;
            var fileName = Path.GetFileNameWithoutExtension(file);
            string delimiter = findDelimiter(file);
            if (delimiter == null)
            {
                Console.WriteLine("File not found:{0}.csv", directoryPath);
            }
            LogWriter.LogData("Info", "", directoryPath, fileName, "", -1, true, numberOfErrors, "", -1, "");
            myParser.Parse(file, delimiter, directoryPathToSave, fileName, objModel, directoryPath, ref numberOfErrors);
            LogWriter.LogData("Info", "", directoryPath, fileName, "", -1, false, numberOfErrors, "", -1, "");
        }

so there is the myParser.Parse function....in which i create the instances like this:

Element element = new Element();

then i set the name, ID and Type....then i save it...

thats all

Aucun commentaire:

Enregistrer un commentaire