1

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.

Patrick.SE
  • 4,444
  • 5
  • 34
  • 44
  • You can use Protected methods, which only allow access from subclasses. Not sure if this is what you're after. – dcp Feb 12 '14 at 16:55
  • Could you post some code--at least just an outline so that we can see the class structure? Looking just at the description, I'm having trouble visualizing just what you're trying to do. – ajb Feb 12 '14 at 16:58
  • I've expanded on my question. – Patrick.SE Feb 12 '14 at 17:03
  • You will have to choose the access modifiers out of the Java possibilities and Java does not enable you to specify a single "friend" class - the Java way of doing this are inner classes. – Smutje Feb 12 '14 at 17:05
  • @Smutje If you edit your main answer I'll accept it for visiblity. – Patrick.SE Feb 12 '14 at 17:07
  • It's worth noting that even in C++ the inner class approach would be preferred on this case. We usually avoid `friend`ship in C++ as much as possible as it's usually not necessary and bad for encapsulation ( Even worst than inheritance ). – Tiago Gomes Feb 12 '14 at 17:14
  • I've moved my comment about using an inner class from the comments down to an answer below and modified it slightly because it sounds like you wanted it to be "private" after I read more of the conversation. Please accept my answer if that is indeed what you were trying to do. :) – Scott Shipp Feb 12 '14 at 17:17

4 Answers4

0

One should have minimal required access to the object and its elements. If you don't require access to entity and it makes sense to have it's access through Vertex only, Make all object handles and methods private within Vertex.

Vivek Vermani
  • 1,934
  • 18
  • 45
0

First, I would ask myself if it is feasible to expose the Entity class at all. After that, you can make the Entities methods private, your Vertex class can still access them and everybody else don't - compares to protected methods which could be accessed by classes in the same source code package.

Edit

You will have to choose the access modifiers out of the Java possibilities and Java does not enable you to specify a single "friend" class - the Java way of doing this are inner classes.

Smutje
  • 17,733
  • 4
  • 24
  • 41
0

I think want you want is simulate the c++ friend modificator in java look at this this question. Hope it helps.

Community
  • 1
  • 1
emcas88
  • 881
  • 6
  • 15
0

You can use an inner class. The inner class has access to its containing class' variables and methods, and vice-versa. If you would like only the outer class to have access to the inner class, you can make the inner class private. Then not even other classes in the same package could call Outer.Inner (or in your case, Vertex.Entity). See docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Scott Shipp
  • 2,241
  • 1
  • 13
  • 20