Hiding your insides from the rest of the world

Xojo has no “friend” scope (see https://www.ibm.com/support/knowledgecenter/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr044.htm)

In other words there is no way to indicate that one class or method can call the protected / private methods of another class. Either the methods are public, and any other code can use them, or they are protected so only the class & subclasses can use them, or private and then only that class can access them.

Sometimes you want “friend” so other classes in a related set of classes can call the protected methods of the other classes in that same set.

I know this all sounds a bit theoretcial but imagine you write a PDF Document Class. And that PDF class has other classes that make up the contents of the PDF – things like PDF Page, and PDF pages might have PDF Text Objects, PDF Graphic Objects, etc

And there could be reasons that a PDF document needs to be able to use methods from PDF Page – but you dont want those methods exposed to every other bit of code in anyones project.

So how do you hide those details from code ?

If all you do it put all the PDF classes ina folder in Xojo then that wont suffice. Folders have no effect as far as the compiler is concerned.

What if you put everything in a module and make all the classes in that module global ?

That would still leave every public method usable and callable by any code.

But, here’s the trick. You CAN put an interface inside the module.
And that interface can be PRIVATE. This means that ONLY classes and code IN the module can access that interface.

And, since interfaces CAN be satisfied using PRIVATE methods you CAN then implement the interface with private methods.

When you combine these two things you get a way for all the classes & code in the module to call the private methods of the classes in the module by casting to the private interfaces and NOTHING outside the module can access them because they can use the private interface, nor can thy directly call the private methos of the classes.

“Friend” scope might still be nice though

The technique outlined only lets you handle hiding within the scope of a single module and if you needed this to work between two separate modules you’re unlikely to find quite such an elegant solution.

Friend would make that much more possible.

2 Replies to “Hiding your insides from the rest of the world”

  1. We stopped putting classes into modules because, under certain circumstances (which are lost in the depths of time), it screwed up Git repositories. Dunno whether that’s still the case though.

    An interesting solution to the lack of “friend”. I will try it out.

    1. Best not be as I’ve used them extensively in Git repo’s
      Cant say I’ve ever had any issues with them in Git in any client project

Comments are closed.