OO Design Question
Class A and Class B are used by different parts of the same large application. I am refactoring part of the code that takes B’s, but needs A’s. They have a function:
private A getAfromB(B b);
Upon seeing this function I immediately thought, self I thought, why isn’t this a constructor for A? This is a fairly complicated function and has a fair amount to do with the internals of A, but this may be the only place where that conversion is needed. So my question is, do I leave this function private at the point of use, never to be seen by the outside world, or do I add a public constructor to A where it can be reused in the future, but might cause confusion to future users of A, or worse add odd Library dependencies (e.g. B needs to be defined in A.java even though it is in an entirely different package)?
December 13th, 2006 at 21:43
My OO suggestions were lost because I apparently can’t do basic arithmetic. I’m not quite sure how that happened.
Anyway: I was trying to say that private or public, it sounds like A has to have a dependency on B if you want this function. The proper place for the function is inside A, if it requires detailed knowledge of A’s innards and those innards shouldn’t be private.
Now, you can still get away from A’s library having a dependency on B if you want. Make the getAfromB take a B-like interface defined in A’s package. This way, the individual user can choose to have a wrapper/facade that supports that interface and gets the information from B if they want without actually tying A to B in the library code.
If the need for this function is really pretty obscure, then you could just make it private and take most of the worries away. You can still do the interface thing for yourself, of course.
December 14th, 2006 at 10:16
meow
December 14th, 2006 at 10:38
Meow! You speak our language? Meow meow *MEOW* meow meow!
December 14th, 2006 at 14:54
I was gonna ask, will B ever change? But now I see Mr. Hannemann has already taken care of that, so instead I will say MEOW. Also, I can do basic arithmetic.
December 14th, 2006 at 23:28
Competely irrelevantly, Erin, I thought of you when I saw this picture. Boat of scooter?
December 15th, 2006 at 11:21
I ended up looking at some other samples of A being used, and they have this odd static member that acts as a sort of mini factory. So I went ahead and wrote another one that takes B’s. It is actually kind of clever, because it is no harder to write than a constructor, but it lets you refactor it into a real factor at some point in the future if you need to. On the other hand it might just be needlessly complex. I was a little afraid that it would confuse the next user, but since that seems to be the way these people do things…
Also, math thing updated so you can hit the back button and get your comment back.
December 18th, 2006 at 04:45
Also, math thing updated so you can hit the back button and get your comment back.
Yay! Wonderful!
As to the actual question, I wondered about a static factory method, but that still ties A to B. It sounds like that’s not the end of the world, though, so that’s OK.