'Friends' equivalent for Java? [duplicate]

2019-01-18 08:59发布

having a little architectural trouble here.

In C++, we have the notion of 'friends,' where such friend classes can access private members.

So, I'm deving a Java app and trying to adhere to the MVC architecture. I've got a controller class that manages graph connectivity between 'map_objects.' I'd like to hide the function in the DTO 'map_objects' that actuall sets up these connectivities, by using this controller class.

(Ie, even if the controller class implements the required functionality of setting up connectivities, the 'users' can still access setter/getter functions in the the DTO directly to set them up themselves.)

Are there any design patterns or tips in this regard? (Or have I totally mucked up?)

DUPLICATE Is there a way to simulate the C++ 'friend' concept in Java?

2条回答
2楼-- · 2019-01-18 09:26

You might want to use interface segregation - that is, let the class implement different interfaces, and only pass out references to the appropriate (smaller) interfaces to the different clients.

查看更多
狗以群分
3楼-- · 2019-01-18 09:39

(Un)fortunately, there is no direct C++ friend equivalent in Java. However, the Java access level modifiers can assist you. In particular, private or package private (AKA package protected, or "default") may help.

查看更多
登录 后发表回答