Take the 2-minute tour ×

I have the following situation. I have a factory method that returns a matrix. This matrix can be calculated at two levels of accuracy: level1 and level2. In order to calculate the matrix, I need to pass a bunch of information to the factory method, and this information is nicely packed in another object Info. Info is passed to the factory method, and the matrix is returned.

Info contains details that are needed for both levels of accuracy. a set of these details are used only by level1. If level2 is needed, info must contain additional data which are used to evaluate level2 accuracy.

I have two strategies to perform the decision:

  1. I pass the Info object to the factory method. If it finds the info for level2, it computes level2 accuracy. If it finds only info for level1, it computes level1.
  2. you pass the factory method both the Info data and the requested accuracy level, then use only the Info contents useful for the appropriate level. There may be a case where level2 is requested, but Info does not contain any level2 information. In that case the program will throw an exception.

Which option would you consider better ?

share|improve this question
I promise I will accept some answers as soon as I'm back. –  Stefano Borini Jun 6 '11 at 15:35
add comment

4 Answers

up vote 3 down vote accepted

I would go with the second option. I would think it's up to the calling function to decide what level it wants, and also if that level is appropriate (it's the caller's responsibility to ensure that if it requests level2, the request can be met with the Info object it gives).

Some pseudo-code of how I'd write it:

if /*there's enough data in Info for level2*/ then
    matrix := MatrixFactory(Info, level2);
    raise NotEnoughInfoForLevel2Exception;
    of maybe you could do 
    matrix := MatrixFactory(Info, level1)
    or something else to handle this problem
share|improve this answer
I had exactly the same sensation. I felt like I was misusing the Info object by granting it two responsibilities: carrying information and controlling flow. –  Stefano Borini Jun 6 '11 at 15:47
add comment

Definitely #2, because it exposes the behavior to the caller rather than hiding important details.

share|improve this answer
"Explicit is better than implicit." –  Tom Anderson Jun 6 '11 at 17:32
add comment

I think that the second option is more sutied to an object oriented solution like yours (its a lot less processing needed), and exceptions should rise when you expected something that you did not received.

Also, if you see this kind of situation (level1 , lever2, levelX) an strategy pattern could also help to enhace your code.

share|improve this answer
add comment

It depends whether the two matrix types are fully compatible on the outside or not. If they are, omitting the extra parameter could be justified, but if they are not interchangeable for all intents and purposes, I'd go with option 2.

share|improve this answer
add comment

Your Answer


