I'm using Java. I want to have a setter method of one class that is only accessible to an "owning" object of another class. I'll be more specific:
I have Node and Port interfaces. Every node has a set of Ports, and each Port belongs to a single Node. A Port may be enabled or disabled, handled with a simple boolean property. However, I only want to allow the Node that "owns" the Port to set its enabled/disabled property. This should happen when Node.fixYourPorts() is called (OK, it has a different name, but I'm trying to simplify).
So, I want everybody to be able to call port.isEnabled(), but only the owning Node should be able to call port.setEnabled().
I can think of a few ways to do this, but I'm not sure which is best, and I'm concerned that I may be thinking about this problem the wrong way. Ideas so far:
Each Node implementation would need a matching Port implementation in the same package, and PortImpl.setEnabled() would be package-access. Doesn't guarantee that one Node won't change another Node's Ports, but that's close enough for me. The Node code is much more under my control than any random client code that may get a Port instance.
Instead of programming to the interface Port, program to an abstract base class, AbstractPort which is a static nested class inside the interface Node. This base class would provide the setter method, so then Node implementations could call it. Not sure if this would even work.
Write another layer of code on top of all of this that the client would not be able to poke through. Add setEnabled to the Port interface. The layer of code on top would make sure the setter is not exposed.
Add the setter to the interface. If you as the client enable or disable a port yourself, then you need to be prepared for things to not work correctly. Add a comment to this effect.
This is all complicated by the fact that Node and Port implementations will be persisted to a DB using Hibernate.
Any help?