1

I have been using the pico container in java to do DI for a minecraft plugin framework I designed.

The plugins have event listener methods that are defined in interfaces, one method per interface.

If a particular class wants events when players join and leave the server, it implements the two particular interfaces; in this case IPlayerLoginEvent and IPlayerQuitEvent.

I then have a class in the frameworks which takes each type of event interface as a constructor injected argument.

This container class is tagged with @Listener for the craftbukkit server software to call it.

The wrapper class wraps the data craftbukkit sends in with a framework-specific class, making it possible, at least in theory, to write server-agnostic plugins.

Small example:

The plugin AwesomePlugin has a class PlayerHandler which implements IPlayerLoginEvent and IPlayerQuitEvent. The framework needs then to construct instances of each of the classes "PlayerLogin" and "PlayerQuit", passing in the PlayerHandler as a constructor argument. The PlayerLogin and PlayerQuit instances will in turn be registered with CraftBukkit as event listeners.

The way this is currently implemented using pico, can be seen here:

https://github.com/Runsafe/Framework/blob/master/src/no/runsafe/framework/event/EventEngine.java#L32

That code does look terrible and I have not been able to find a more elegant solution for this scenario, so I now beseech your guidance :)

4

1 に答える 1

2

質問の意味がよくわかりませんが、いくつかのピコ イベントのパターンについて説明できます。

まず第一に、イベントはコンテナー中心であり、物事を接続するためだけに pico を使用することはありません。すべてのイベント コンシューマーがコンテナーに追加されます。各コンシューマーは、イベント obj を取得するために少なくとも 1 つのメソッドを実装する必要があります。ジェネリック Event クラスが渡された単一のインターフェイスを使用することも、さまざまなイベント タイプに対して多数のインターフェイスを使用することもできます。

したがって、コンテナー内にコンシューマーのリスト (または、いくつかのイベント タイプの複数のリスト) があります。コンシューマーのリストをイベント ブロードキャスター オブジェクトに注入することができます (明らかに、コンテナーの作成ごとに 1 回実行され、ブロードキャスターはどこかから何らかの方法でイベントを受け取ります)。 、次にリストを反復処理して関連するイベント ハンドラー メソッドを呼び出すか、コンテナーのカスタム ライフサイクル呼び出しを行う (いつでも実行でき、実行時に追加されたコンシューマーをトリガーします。呼び出しジョブはコンテナー自体によって実行されます)。

場合によっては、イベント obj と関連するすべてのコンシューマーを追加して、イベントごとに新しいイベント コンテナーを作成することもできます。

Waffle フレームワークには、ピコ コンテナーのジャグリング、イベント処理などの最も優れた例が含まれています。

于 2012-10-31T19:10:48.923 に答える