簡単な説明
私はいつも、Javaが既製の委任メカニズムの欠如を心配してきました。幸いなことに、私は最近しばらく時間があり、参照のためにリフレクションを備えた簡単な代表団モジュールを書きました。
モジュールAPI
パブリッククラスDeLegater()//空のパラメーター構築、このクラスはデリゲートインスタンスを管理し、デリゲートメソッド//静的メソッドデリゲートを追加し、整数値IDを返してメソッドとパラメーターで構成されるインスタンスを表します。失敗した場合は、-1を返します。 public synchronized int addFunctionDelegate(class <?失敗した場合は、-1を返します。 public synchronized int addFunctionDelegate(Object Srcobj、string methodname、object ... params); //整数IDに基づいてデリゲートインスタンスからメソッドデリゲートを削除し、パブリックシンクロニングブールのremovemethodが成功したかどうかを返します。 InvokeAllMethod(); //パラメーターテーブルをパラメータータイプに変換しますprivate class <? RefMethod、Object [] Params)// DelegateNodeクラスは、メソッドインスタンスやパラメーターテーブルを含むオブジェクト構造を使用しない場合の静的メソッド委任について説明します。クラスDelegatenode(Object Srcobj、Method Refmethod、Object Params)// DelegateNodeクラスは、クラスインスタンス、メソッドインスタンス、パラメーターテーブルを含むオブジェクト構造を使用する場合のインスタンスメソッド委任を説明します。
ソースコード
java.lang.reft.invocationTargetException; Import Java.lang.Reflect.Method; Import Java.util.hashtable;/** DelegaterクラスはRTTIとリフレクションを使用してJava*@author 3-way brick*/public Class int intec intec intect intected intect intect intect intect intect intect intect intect intect int integ intect intect intect intect edector brick*/可変ハッシュテーブル<integer、delegatenode> nodetable; // IDと対応する代表者のコンテナを管理するpublic delegater(){nodetable = new Hashtable <integer、delegatenode>();} // static method dealegate public synchronized int adddeleass(Object name、<? {class <?> [] paramtypes = getParamtypes(params); method refmethod; if((refmethod = getdstmethod(srcclass、methodname、paramtypes))!= null){register ++; nodetable.put(register、new delegateNode(Refmethod、params);ダイナミックメソッドデリゲートpublic synchronized int addFunctionDelegate(Object srcobj、string methodname、object ... params){class <?> [] paramtypes = getParamtypes(params); method refmethod; if((refmethod = getdstmethod(srcobj.getClass()、methodname、paramTypes) {Register ++; nodetable.put(Register、new DelegateNode(Srcobj、RefMethod、Params)); return Register;} elter {return -1;}} //パブリック同期removemethod(int registerid){(nodetainskey(registarid){; true;} return false;} //デリゲートメソッドを実行します。 {class <?> [] paramtypes = new class <?> [params.length]; MethodName、class <? SecurityException e){system.out.println( "can found method:"+methodname+"、存在して表示されていることを確認してください!");} class delegatenode {object srcobj; method refmethod; object [] object [] parmors; public delegatenode(method refmethod = object refmesod) this.params = params;} public delegatenode(object srcobj、method refmethod、object [] params){this.srcobj = srcobj; this.refmethod = refmethod; this.params = params;} public void invokemethod(){try {refmethod.invoke(srcobj、params);} catch(IllegalAclargumentexception | InvocationTargetException E){System.out.Out.Println( "+Refmethod.Tostring(");モジュールテスト
public class Delegatertest {public void showinfo(){system.out.println( "Hello Delegate!");} public void showcustominfo(system.out.println(info);} public static void showstaticinfo(){system.out.out.out.out.out.out.out.out. showcustomstaticinfo(string info){system.out.println(info);} public static void main(string [] args){Delegater dele = new Delegater(); Delegatertest Tester = new Delegatertest(); int id = dele.AddFunctionDelegate(Tester、 "Showinfo"); dele.AddFunctionDelegate(Tester、 "Showcustominfo"、 "Custom!"); dele.AddFunctionDelegate(DeLegatertest.Class、 "ShowStaticInfo"); dele.Addfuncti onDelegate(DelegaterTest.class,"showCustomStaticInfo","StaticCustom!");dele.invokeAllMethod();dele.removeMethod(ID);System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------実行結果:
staticcustom!
staticdelegate!
カスタム!
hellodelegate!
---------------------
staticcustom!
staticdelegate!
カスタム!
その他の問題
一部のパブリック方法では、レジスタ変数のスレッドの安全性を確保するために同期して使用して、複数のスレッドのために間違いを犯さないようにします。
返品値の代表に対して警告が発行されますが、モジュールは依然としてそのような代表団を受け入れますが、代表団の実行時に返品値は取得されません。
追加された代表者の最大値は、integer.max_value-integer.min_valueの後のフォールトトレランスプロセスです(一般に、代表者を必要とする関数はそれほど多くありません。
委任された実行は乱れ、パフォーマンス要件が必要な場合は、委任された関数をブロックしないようにしてください。そうしないと、他の委任機能の実行に影響します。
他の質問を投稿して議論することができます。
要約します
上記は、Reflectionを介してJavaにデリゲートメカニズムコードを実装することに関するこの記事のすべての詳細な説明です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このウェブサイトで他のJava関連のトピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!