I've always wondered. I have this Vertex class that's part of a generic Graph class. This Vertex class owns an object that's an entity. In my system everything happens trough the Vertex, you can't directly access the entity object with a getter. I realized though that I had to create public methods in my entity class so they can be called from the Vertex class. Is there a way to only expose methods to a class that owns said object?
Because right now I can instantiate an Entity and use it's public methods, but it doesn't make sense outside of the Vertex class. I don't know if there's a pattern or something people do to only let owners use methods of whatever they hold.
I'm using Java right now, but C++ is fine too. I believe in C++ you can use the friend keyword.
//Vertex.java
public class Vertex
{
private NodeDrawable _node;
...
}
//NodeDrawable.java
public class NodeDrawable
{
private disable();
}
I'd like to make Vertex the only class that's allowed to access NodeDrawable methods. Inner classes are cool, but I don't like having multiple classes in a single file.