0

I have a problem where I have a main class which solves a numerical problem. For simplicity, assume that it solves Ax=b. Now, I want the user the ability to choose the method to solve it. There are thousands of options and each option has thousands of options within it.

My idea was to design it as follows: create a main class and then create subclasses for each method and subsubclasses for the details of each methods (which might interact via inheritance).

For instance, I envisage the user to do something like- Model.method='CG' Model.preconditioning=off and then Model.Solve and in the Model class, there is a CG subclass which runs. Within CG there are methods CG_Precond and CG_NoPrecond which run depending on the preconditioning being on or off. (Assume that the methods are wildly different). So, in essence, the user is running Model.CG.CG_NoPrecond.

Is this good design? Should nested classes be avoided?

One important note is that other than the Model class, all of the subclasses contain only methods and no data of their own (other than what is returned).

I spent some time reading some really beautiful answers on SO and my problem ( I believe) aligns with the requirements of the accepted answer of Why/when should you use nested classes in .net? Or shouldn't you?.

Community
  • 1
  • 1
  • Seems like you are in need of the Strategy (http://sourcemaking.com/design_patterns/strategy) design pattern. – LordDoskias Aug 31 '13 at 21:52
  • Might it be easier to just give the class a 'ModelSettings' struct with the information it needs? – Tharwen Aug 31 '13 at 22:01
  • So you have a tree of methods for solving problems. The tree has thousands of branches, with a depth of three? You will likely have to generate the tree somehow. Seems like the nested classes will be much easier to read than the other way you mentioned... – Dru Sep 01 '13 at 01:00
  • I didn't follow your argument about trees. I have many methods and each method has a few knobs you can turn. –  Sep 01 '13 at 01:13
  • It sounds like you might be able to solver your problem by dividing the problem in sub-problems. You should do well, if you try to avoid duplicate code, god objects, circular dependencies, insanely long methods and other other bad conventions. Focus on keeping the code readable and maintainable. – Sami Korhonen Sep 01 '13 at 20:12

1 Answers1

0

First, you should create a class Solver and use the Strategy Pattern to create subclasses which represent the different methods to solve the problem.

The options and suboptions are a harder thing to do right. If i got you right, then CG_Precond and CG_NoPrecond should be subclasses of a CG (which is also a subclass of Solver) as they seem to share some inner logic.

If the options are like predefined values for the different methods where each method requires other values and type of values, then becomes more difficult. There i would like you to present some more examples of options, suboptions and so on.

valenterry
  • 757
  • 6
  • 21