创建循环通用引用

发布于 2024-08-28 05:26:45 字数 772 浏览 11 评论 0原文

我正在编写一个应用程序来在对等网络中进行一些分布式计算。在定义网络时,我有两个类:P2PNetwork 和 P2PClient。我希望这些是通用的,因此具有以下定义:

P2PNetwork<T extends P2PClient<? extends P2PNetwork<T>>>

P2PClient<T extends P2PNetwork<? extends T>>

使用 P2PClient 定义 setNetwork(T network) 方法。我希望用这段代码描述的是:

  1. P2PNetwork 是由 某种类型的客户端
  2. A P2PClient 可能只属于一个 其客户端包括的网络 与此客户端相同的类型( 循环引用)

这对我来说似乎是正确的,但如果我尝试创建非通用版本(例如

MyP2PClient<MyP2PNetwork<? extends MyP2PClient>> myClient;

和其他变体),我会从编译器收到许多错误。所以我的问题如下:

  1. 通用循环引用是否是 可能(我从未见过任何明确禁止的内容)?
  2. 上述通用定义是 此类通告的正确定义 关系?
  3. 如果有效,它是否“正确” 描述这种关系的方式 (即是否还有另一个有效的 定义,这是风格上的 首选)?
  4. 我将如何正确定义 客户端的非通用实例和 给定正确通用的服务器 定义?

I am writing an application to do some distributed calculations in a peer to peer network. In defining the network I have two class the P2PNetwork and P2PClient. I want these to be generic and so have the definitions of:

P2PNetwork<T extends P2PClient<? extends P2PNetwork<T>>>

P2PClient<T extends P2PNetwork<? extends T>>

with P2PClient defining a method of setNetwork(T network). What I am hoping to describe with this code is:

  1. A P2PNetwork is constituted of
    clients of a certain type
  2. A P2PClient may only belong to a
    network whose clients consist of the
    same type as this client (the
    circular-reference)

This seems correct to me but if I try to create a non-generic version such as

MyP2PClient<MyP2PNetwork<? extends MyP2PClient>> myClient;

and other variants I receive numerous errors from the compiler. So my questions are as follows:

  1. Is a generic circular reference even
    possible (I have never seen anything explicitly forbidding it)?
  2. Is the above generic definition a
    correct definition of such a circular
    relationship?
  3. If it is valid, is it the "correct"
    way to describe such a relationship
    (i.e. is there another valid
    definition, which is stylistically
    preferred)?
  4. How would I properly define a
    non-generic instance of a Client and
    Server given the proper generic
    definition?

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

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

发布评论

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

评论(2

神经暖 2024-09-04 05:26:45

循环通用引用确实是可能的。 Java 泛型和集合 包括几个示例。对于您的情况,这样的样本将如下所示:

public interface P2PNetwork<N extends P2PNetwork<N, C>,
                            C extends P2PClient<N, C>> {
  void addClient(C client);
}

public interface P2PClient<N extends P2PNetwork<N, C>,
                            C extends P2PClient<N, C>> {
  void setNetwork(N network);
}

class TorrentNetwork implements P2PNetwork<TorrentNetwork, TorrentClient> {
  @Override
  public void addClient(TorrentClient client) {
    ...
  }
}

class TorrentClient implements P2PClient<TorrentNetwork, TorrentClient> {
  @Override
  public void setNetwork(TorrentNetwork network) {
    ...
  }
}

...

TorrentNetwork network = new TorrentNetwork();
TorrentClient client = new TorrentClient();

network.addClient(client);

Circular generic references are indeed possible. Java Generics and Collections includes several examples. For your case, such a specimen would look like this:

public interface P2PNetwork<N extends P2PNetwork<N, C>,
                            C extends P2PClient<N, C>> {
  void addClient(C client);
}

public interface P2PClient<N extends P2PNetwork<N, C>,
                            C extends P2PClient<N, C>> {
  void setNetwork(N network);
}

class TorrentNetwork implements P2PNetwork<TorrentNetwork, TorrentClient> {
  @Override
  public void addClient(TorrentClient client) {
    ...
  }
}

class TorrentClient implements P2PClient<TorrentNetwork, TorrentClient> {
  @Override
  public void setNetwork(TorrentNetwork network) {
    ...
  }
}

...

TorrentNetwork network = new TorrentNetwork();
TorrentClient client = new TorrentClient();

network.addClient(client);
心安伴我暖 2024-09-04 05:26:45

如果您进一步定义“某种类型”的含义,即P2P网络的各种“类型”之间的差异是什么,可能会帮助我们回答您的问题。

但是,与其用彼此来表达依赖/循环关系,不如通过引入第三个类 P2PType 来表达可能更容易:

public class P2PNetwork<T extends P2PType> {
    ...
}
public class P2PClient<T extends P2PType> {
    ...
    public void setNetwork(P2PNetwork<T> network) { ... }
}

我可能会忽略一些东西,但我认为这将允许编译器强制 P2PClient 是相同通用类型的 P2PNetwork 的一部分。

然而,如果“类型”不适合表达为面向对象本身,即如果P2PType不是具有方法、多态行为、 ETC。

It might help us to answer you if you further defined what "a certain type" means, i.e. what the differences are between various "types" of P2PNetworks.

But instead of expressing the dependency / circular relationship in terms of each other, it might be easier to express by introducing a third class, the P2PType:

public class P2PNetwork<T extends P2PType> {
    ...
}
public class P2PClient<T extends P2PType> {
    ...
    public void setNetwork(P2PNetwork<T> network) { ... }
}

I might be overlooking something but I think this would allow the compiler to enforce that P2PClients are a part of P2PNetworks of the same generic type.

This approach might fall apart however if the "type" isn't something that is suitable to express as a object-oriented itself, i.e. if the P2PType isn't something that would have methods, polymorphic behavior, etc.

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