java中如何仅显示界面的视图

发布于 2024-12-12 10:48:54 字数 1944 浏览 0 评论 0原文

是否可以仅显示接口的特定视图或在 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 接口的用户不知道需要进行身份验证才能进行交互,甚至不知道 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++ 中的朋友,但我觉得它略有不同。我不想授予使用某种方法的特殊权限。

我希望有两个接口作为 SecureDevice 的一部分:

   public interface SecureDevice
   {
       public interface UserView
       {
           connectWith(SecureDevice d);
       }

       public interface LibraryView
       {
           connectwith(SecureDevice d, Authentication a);
       }
    }

如果确实有两个单独的接口,当我想使用另一个接口时,我必须在它们之间进行转换,并且我将无法保证实现一个接口的对象实际上实现了另一个,所以我必须进行运行时类型检查。

所以归根结底,我只想向用户呈现一个简单的界面,只包含他需要的方法,而不包含他不需要的业务。我该如何做到(或近似)这一点?

谢谢

Is it possible to show only a particular view of an interface or approximate this behavior in Java? For example:

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
}

I would like users of the SecureDevice interface to have no idea that an authentication needs to occur for the interaction to occur or even that the class Authentication exists. They don't need to know the details. They only need to know that the interaction either happened or didn't. For example, a user who obtained two SecureDevice objects might try this:

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.");
    }
 }

but I might define a class like this:

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

or similarly

public class Connector 
{
    private Authentication myAuth;

    public bool connect(SecureDevice a, SecureDevice b)
    {
        a.connectWith(b, myAuth);
    }

}   

This example seems akin friends in C++, however I feel like it is slightly different. I don't want to give special permission to use a method.

I would like to have two interfaces as part of SecureDevice:

   public interface SecureDevice
   {
       public interface UserView
       {
           connectWith(SecureDevice d);
       }

       public interface LibraryView
       {
           connectwith(SecureDevice d, Authentication a);
       }
    }

If there really were two separate interfaces, I would have to cast between them when I wanted to use the other interface, and I wouldn't be able to gurantee that an object implementing one actually implemented the other, so I'd have to do runtime type checking.

So to boil it down, I just want to present a simple interface to the user with only the methods he needs, and none of the business he doesn't need. How can I do (or approximate) this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-12-19 10:48:54

在 Java 中,您无法使用简单的接口来控制对各个方法的访问。另一方面,您可以控制对 EJB 接口中方法的访问,其中只有具有授权角色的用户才能调用方法 - 当然,每个客户端类都会看到公开的方法,但只有授权用户才能成功调用它们

In Java you can't control access to individual methods using a plain interface. On the other hand, you can control access to methods in an EJB interface, where only users with the authorized roles can invoke a method - surely, every client class will see the exposed methods, but only authorized users will be able to invoke them successfully

染年凉城似染瑾 2024-12-19 10:48:54

不。如果您必须提供未经身份验证的界面,那么这就是该界面中需要的内容。

您可以扩展接口,但最终会遇到类似的问题。

另一种选择是接受实际工作的实现,其构造包含实现细节,非常大致如下:

public interface SecurableDevice {
公共布尔 connectWith(连接器);
}

公共类 SecurableConnector 实现连接器 {
公共 SecurableDeviceConnector(SecureDevice sd) { ... }
公共无效 connectWith(d1) { ... }
}

公共类 SecurableDeviceConnector 实现连接器 {
公共 SecurableDeviceConnector(SecureDevice sd, 授权 a) { ... }
公共无效连接(d1){...}
连接

建立移出SecurableDevice;设备调用 connectWith,并在那里进行处理。

连接器实现只能提供给需要它们的用户。

No. If you must present an interface without authentication, then that's what needs to be in that interface.

You could extend the interface, but you end up with a similar problem.

Another option would be to accept an implementation of something that does the real work, the construction of which contains the implementation details, very roughly:

public interface SecurableDevice {
public boolean connectWith(Connector);
}

public class SecurableConnector implements Connecter {
public SecurableDeviceConnector(SecureDevice sd) { ... }
public void connectWith(d1) { ... }
}

public class SecurableDeviceConnector implements Connector {
public SecurableDeviceConnector(SecureDevice sd, Authorization a) { ... }
public void connectWith(d1) { ... }
}

The connection establishment is moved out of the SecurableDevice; the device calls connectWith, and it's handled there.

The Connector implementations can be provided to only the users that need them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文