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

本文是小编为大家收集整理的关于'Friends'相当于Java?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这里有一些建筑麻烦.

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

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

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

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

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

推荐答案

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

其他推荐答案

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

本文地址:https://www.itbaoku.cn/post/627694.html

问题描述

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.