|Think about Loose Coupling|
Private Classes - Same or Separate Files?by Masem (Monsignor)
|on Mar 06, 2002 at 14:54 UTC||Need Help??|
Say I have a class My::A; at the time of the development cycle, it's determined that it would a good idea that there be some class My::B which can only be created through My::A and manipulated by My::A; My::B objects are not passed outside of My::A code. So for all effective reasons, My::B can be considered a private class of My::A.
Now, in C++ or Java, you can use the combination of access restrictions on classes and/or internal classes such that My::B would have been sufficiently hidden from any external code save for My::A. Typically, the My::B class would be in the same file as My::A declarations and definitions.
In perl, it's nay impossible to hide such a class (TTBOMK). So there's two possible options I can see: either put the definition of My::B in My::A's file as:
or to put B into it's own file:
In neither case, My::B isn't protected as the user would still be able to gain access to My::B, so worrying about this issue is a forgone conclusion. Thus, it's now mostly a semantics question -- which is the better or preferred way of doing this? The only advantage that it would see to me is using separate files, it's more obvious during use statements that the user is bringing My::B into the block, as opposed to the single file approach; of course, this again makes My::B more accessible that it might seem.
(Now of course, I put the cavaet that such hiding may be determined by the initial design, but down the road, it may be determined that allowing the user to inherit from My::B may be useful or important, so the fact that such code is not hidden by perl to start may be a good thing.
Update I should state that I fully understand that I cannot 'protect' my class from being instaniated as demerphq points out (and I like his analogy as well). Given that, I'm more looking for how I place the class def into files as to indicate to the user as best as possible that they really shouldn't instaniate the class, and/or to make it fit the perl philosophy more.