patterncsharpModerate
Solving Project Euler challenge
Viewed 0 times
solvingprojecteulerchallenge
Problem
I wanted to consolidate solutions to disparate problems into a single program. I am worried that there is a interface, command pattern, or dictionary solution to what will become a massive switch statement here. However, the main in this program is essentially a meta-program which would switch between the miniature programs that it links to.
This is not an elegant solution, but it seems to mirror the situation quite well, as I see it.
Should I scratch this approach? It seems to me to violate OCP but at the same time, it isn't chaotic, and isn't that much to keep track of, - just tack one more case onto the end, create a new class, and another problem is functional. Should I look into some other structure to make it easier to modify?
static void Main(string[] args)
{
int eulerNumber;
Console.WriteLine("Please enter the number of the Problem you want to solve.");
while (!int.TryParse(Console.ReadLine(), out eulerNumber))
{
Console.WriteLine("Please enter a valid number.");
Console.WriteLine();
Console.WriteLine("Please enter the number of the Problem you want to solve.");
}
switch (eulerNumber)
{
case 1:
//solve problem 1.
break;
case 2:
//solve problem 2.
break;
case 3:
//solve problem 3.
break;
etc. etc. etc.
}
}This is not an elegant solution, but it seems to mirror the situation quite well, as I see it.
Should I scratch this approach? It seems to me to violate OCP but at the same time, it isn't chaotic, and isn't that much to keep track of, - just tack one more case onto the end, create a new class, and another problem is functional. Should I look into some other structure to make it easier to modify?
Solution
If I were to solve this, adding the new code to the switch seems to me like overkill.
I would try writing an application that uses plugins in a searchable directory.
Then you solve each problem in its own plugin and the main program just consists of the logic to find and execute the plugins.
Of course to enhance finding the plugins there some possible designs:
Naming the plugin after the solved problem
Name the plugin file by a given structure so it is obvious which problem is solved:
That has the problem of forbidding user defined names, however it offers efficiency in that you can directly load only the needed plugin if you know the number.
Give the plugin class a problemNumber function
The other solution is to give the plugin class (that you will need anyway for a common interface to query for the solution) a function that returns the problem number.
This allows for arbitrarily named plugins, but also for duplicates and has the disadvantage of loading all plugins that are there to see if the one containing the requested solution is there.
Since you are not that much efficiency bound on this problem I would suggest the second alternative because it has a much nicer OO interface (IMHO).
I would try writing an application that uses plugins in a searchable directory.
Then you solve each problem in its own plugin and the main program just consists of the logic to find and execute the plugins.
Of course to enhance finding the plugins there some possible designs:
Naming the plugin after the solved problem
Name the plugin file by a given structure so it is obvious which problem is solved:
ep-001.dll
ep-002.dll
...That has the problem of forbidding user defined names, however it offers efficiency in that you can directly load only the needed plugin if you know the number.
Give the plugin class a problemNumber function
The other solution is to give the plugin class (that you will need anyway for a common interface to query for the solution) a function that returns the problem number.
This allows for arbitrarily named plugins, but also for duplicates and has the disadvantage of loading all plugins that are there to see if the one containing the requested solution is there.
Since you are not that much efficiency bound on this problem I would suggest the second alternative because it has a much nicer OO interface (IMHO).
Code Snippets
ep-001.dll
ep-002.dll
...Context
StackExchange Code Review Q#38019, answer score: 10
Revisions (0)
No revisions yet.