作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以仅显示界面的特定 View 或在 Java 中模拟此行为?例如:
public interface SecureDevice
{
bool connectWith( SecureDevice d ); // visible to people who have a SecureDevice object
bool connectWith( SecureDevice d, Authentication a ); // somehow only visible to classes with permission
}
我希望 SecureDevice 接口(interface)的用户不知道交互需要进行身份验证,甚至不知道 Authentication 类的存在。他们不需要知道细节。他们只需要知道交互是否发生。例如,获得两个 SecureDevice 对象的用户可能会尝试以下操作:
public void establishConnection(SecureDevice d1, SecureDevice d2 )
{
Connector c = new Connector();
if( false == d1.connectWith( d2 ) )
{
System.out.println("Couldn't connect to directly");
}
else if ( false == c.connect( d1, d2 ) )
{
System.out.println("Couldn't connect using Connector.");
}
}
但我可能会定义一个这样的类:
public class Computer implements SecureDevice
{
private Authentication auth;
public bool connectWith( SecureDevice d )
{
return d.connectWith(d, auth);
}
private? bool connectWith(SecureDevice d, Authorization a)
{
// check the authorization, do whatever it takes to connect, etc.
}
}
或类似
public class Connector
{
private Authentication myAuth;
public bool connect(SecureDevice a, SecureDevice b)
{
a.connectWith(b, myAuth);
}
}
这个例子看起来很像 C++ 中的 friend ,但我觉得它略有不同。我不想授予使用方法的特殊权限。
我希望有两个接口(interface)作为 SecureDevice 的一部分:
public interface SecureDevice
{
public interface UserView
{
connectWith(SecureDevice d);
}
public interface LibraryView
{
connectwith(SecureDevice d, Authentication a);
}
}
如果确实有两个单独的接口(interface),当我想使用另一个接口(interface)时,我必须在它们之间进行转换,并且我无法保证实现一个接口(interface)的对象实际上实现了另一个接口(interface),所以我必须进行运行时类型检查。
所以归根结底,我只想向用户呈现一个简单的界面,只包含他需要的方法,而不包含他不需要的业务。我该如何做到(或近似)这一点?
谢谢
最佳答案
在 Java 中,您无法使用简单的接口(interface)来控制对各个方法的访问。另一方面,您可以控制对 EJB 接口(interface)中方法的访问,其中只有具有授权角色的用户才能调用方法 - 当然,每个客户端类都会看到公开的方法,但只有授权用户才能成功调用它们
关于java - java中如何仅显示界面的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7922841/
我是一名优秀的程序员,十分优秀!