Nick Malik said:
Hello Learner,
I'm confused by your reticence to use "internal". A class is an
abstraction. An object is an instance of a class, complete with data. One
of the underlying assumptions about OOP is that you should hide the objects,
because that's where the real power lies. Hiding a class is not as
important and leads to some interesting issues (which you've discovered).
Yet you are worried not only about hiding the objects, but also about hiding
the class from other code in the same namespace. This seems
counter-intuitive to me.
From where I sit, this is how it works: The "parent" class creates a private
object of a "child" class type. No other code has access to this particular
object. Sure, other classes can instantiate the child class. (Other
classes can instantiate the parent class too). Unless you are buried under
a serpentine mix of Singleton objects and Factory Methods, you should have
little or no negative consequences to using this idea to reduce the
complexity of your class without sacrificing OOP principles.
Hi Nick,
Let me give an example of what I'm thinking of here:
I have a namespace called FooProtocol. In this namespace is a bunch
of classes which enable the use of a communication protocol called
FooProtocol.
In the FooProtocol namespace I have a class called Session, which
takes care of a client session of FooProtocol. Now, Session reads
packets from the server and fires events after parsing these packets.
In the code of my Session class, I see a lot of methods which have
similar names like:
ProcessFirstPacketType
ProcessSecondPacketType
ProcessThirdPacketType
etc.
Seeing this, I think so myself, I really should make a Processor or
maybe PacketParser class, whose only purpose is to parse these
packets.
Now, this new class should not be visible to anything but Session,
since it is simply an implementation detail of Session.
Further, I may with to create a Processor class as an implementation
detail of another class in the FooProtocol namespace. In this case,
I'd have to name one SessionProcessor and the other
OtherClassNameProcessor, which in my mind shows without doubt that
these classes should be private nested classes of their relevant
classes.
puzzled by your puzzlement,
--- Nick
Hope that makes sense!
Cheers