I am having difficulty figuring out an efficient (thread-safe) code design for the following problem. I have been at it for some time now and would really appreciate some advice and input on how best to approach this.
I am basically searching for various patterns inside a data set. I would like to have one "master" class. I call this class Patterns. I am thinking of making this class a singleton pattern or a static class. I wish to have only one instance of this master class. I am coding this class as a singleton pattern (pattern by Jon Skeet) at this time.
public sealed class Patterns
{
private Patterns()
{}
public static Patterns Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
// ... create a list to hold data for internal use
List<Point> Data = new List<Point>();
}
internal static readonly Patterns instance = new Patterns();
}
}
I have chosen a singleton pattern because:
- I want the singleton to hold nested classes; one class for each pattern.
- I want these nested classes to be instance classes (i.e. not static)
- I would like the singleton to instantiate the nested classes, perhaps using a factory pattern, as needed.
I am not sure whether my reasoning here is correct so any advice would be greatly appreciated.
Now I plan to add the various pattern classes as nested classes because patterns do not make sense outside the master Pattern class. Also, IPattern is an interface for the patterns.
public sealed class Patterns
{
private Patterns()
{}
public static Patterns Instance { get { return Nested.instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
// ... create a list to hold data for internal use
static List<Point> Data = new List<Point>();
// ... indices to track occurences of each pattern
static int AIndex = 0;
static int BIndex = 0;
static int CIndex = 0;
}
internal static readonly Patterns instance = new Patterns();
}
// ... Methods
public bool SearchForPattern(Point dataPoint)
{
// ... add data to list
Data.Add(dataPoint);
}
// ... nested pattern classes
class PatternA : IPattern
{
static int = some integer
static double[] arr = new double[];
// ... do some work
}
class PatternB : IPattern
{
// ... do some work
}
class PatternC : IPattern
{
// ... do some work
}
}
The List Data is static because I want the data to be retained throughout the lifetime of the application. Same for the static fields in the nested classes.
This is where I now begin to really get lost. In the method SearchForPattern(Point dataPoint) I would like to do the following:
public bool SearchForPattern(Point dataPoint)
{
// ... add data to list
Data.Add(dataPoint);
bool haveA = PatternA.SearchForA();
if (haveA)
{
PatternA a1 = new PatternA(); // the number after a will change as other A patterns are found; as per AIndex value
AIndex++;
}
... and so on for other patterns
}
So here are my questions. Please note that I am fairly new to C# and I am trying to learn.
Is this the best (i.e. most computationally efficient) approach to take with this problem? If not, can you please suggest an alternative approach?
I do not know how to incorporate the factory pattern for the instantiation of the nested pattern classes. Should this be done? If so, can you suggest how?
Am I missing anything here that I need to consider but have not considered so far?