c# - Different algorithm for different inputs -
I am facing a situation. I want to know whether this is a design pattern or not. This is as follows:
- The user is presented on a web interface with a variety of algorithms for a process
- User selection is stored in database
- Now, the application should perform different computations depending on the selected algorithm.
What is a good strategy to implement it? Right now we're doing -
- a reference DB table which is the same class name in the algorithm type and code (for example, the quick sort algorithm we store quicksort.) . In the code, get the algorithm type and use the reflection to instantify the appropriate algorithm type.
-
; In C # weSystem.Reflection.Assembly type = System.Reflection.Assembly.LoadFile (. System.Reflection.Assembly.GetExecutingAssembly () Location.ToString ()) using the same code below Foreach ( Type T type) if (t.Name == classname) createinstanceof (t) // classnames is a list of all class types that have been loaded in the DB from the reference table.
My gut feeling is that there should be a simple / better way to do this because it is a very standard problem. I know - but what I want is to remove the simple and possibly manual tasks.
To avoid that you can use the interface + algorithm name storing image database
Create an interface as IMySortingAlgorithms,
public interface IMySortingAlgorithms {string name {meets ;; } String [] sort (string [] input); }
Now, using a factory reflection sorting algorithm
public static class MyAlgoFactory {writing to obtain private static Dictionary & lt ;; String, IMySortingAlgorithms & gt; M_dict; /// & lt; Summary & gt; /// For all assmeblies in the current application domain, /// I get all kinds of objects that implement IMySortingAlgorithms /// & lt; / Summary & gt; Fixed MyAlgoFactory () {var type = Typing (IMySortingAlgorithms); M_dict = AppDomain.CurrentDomain.GetAssemblies (). SelectMany (s = & gt; s.GetTypes ()). Where (p = & gt; {return type. Aisasgneblfrom (P) & amp; P! = Type;}) Select (T = & gt; Aktivitr. Bnaaaindstorting as algorithmic) ToDictionary (i = & gt; i.Name ); } Public static IMySortingAlgorithms GetSortingAlgo (string name) {return m_dict [name]; }}
All your sorting algorithms can now implement this interface.
Public category MySortingAlgo1:. IMySortingAlgorithms {#region IMySortingAlgorithms Member Public String Name {{Return to "MySortingAlgo1"; }} Public String [] Sort (String [] Input) {New Throw Impressed Expansion (); } #endrian}
In this way you do not need to add class names to the database whenever you create a new class for sorting.
The following non-Linq version is MyAlgoFactory
/// & lt; Summary & gt; /// For all assmeblies in the current application domain, /// I get all kinds of objects that implement IMySortingAlgorithms /// & lt; / Summary & gt; Fixed MyAlgoFactory () {m_dict = New Dictionary & lt; String, IMySorting algorithm & gt; (); Var type = typef (IMIScring algorithm); foreach (AppDomain.CurrentDomain.GetAssemblies assembly ASM ()) {foreach (Type P (in asm.GetTypes in)) {if (type.IsAssignableFrom (P) & amp; & amp; P = type!) {IMySortingAlgorithms algo = Activator IMySortingAlgorithms as CreateInstance (P); M_dict [algo.Name] = Algo; }}}}
Comments
Post a Comment