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 ?


4 Answers 4


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
  • 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. Jun 6, 2011 at 15:47

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

  • 1
    "Explicit is better than implicit." Jun 6, 2011 at 17:32

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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.