Java 的“朋友"等价物? [英] 'Friends' equivalent for Java?

查看:48
本文介绍了Java 的“朋友"等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里在架构上有点问题.

having a little architectural trouble here.

在 C++ 中,我们有朋友"的概念,这样的朋友类可以访问私有成员.

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

所以,我正在开发一个 Java 应用程序并尝试遵循 MVC 架构.我有一个控制器类,用于管理map_objects"之间的图形连接.我想通过使用此控制器类隐藏 DTO 'map_objects' 中实际设置这些连接的函数.

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.

(即,即使控制器类实现了设置连接所需的功能,用户"仍然可以直接访问 DTO 中的 setter/getter 函数以自行设置它们.)

(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?)

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

推荐答案

(不)幸运的是,Java 中没有直接的 C++ 朋友等效项.但是,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.

这篇关于Java 的“朋友"等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆