'Friends'相当于Java?[英] 'Friends' equivalent for Java?




在C ++中,我们有"朋友"的概念,这些朋友可以访问私人会员.

所以,我正在使用一个Java应用程序,并试图遵守MVC架构.我有一个控制器类,可以管理" map_objects"之间的图形连接.我想通过使用此控制器类.


这方面有任何设计模式或技巧吗? (还是我完全陷入困境?)

重复 有没有办法在Java中模拟C ++'朋友'概念?


(un)幸运的是,Java中没有直接的C ++朋友.但是, java访问级别级别修饰符可以为您提供帮助.特别是,私有或软件包私有(又称保护软件包或"默认包")可能会有所帮助.


您可能要使用接口segregation 不同的接口,仅将适当(较小)接口的引用传递给不同的客户端.



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?


(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.


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.