2016-04-05 9 views
0

私は自分のSDKのクライアントに提供するのに最適なAPIを見つけることを試みています。基本的に、典型的なユースケースは1つのアプリケーションで1つのセッションインスタンスと1つのレンダラーになりますが、これは将来変更される可能性があります。クライアントAPIを実装する最善の方法はどれですか

重要な点:将来的に見通しが立てやすく、将来的に見通しが良いユーザーから使いやすいものでなければなりません。

方法A:

/** 
* The user have to create and maintain the module 
* 
* pros: 
* -module explicitly added at construction time 
* -uses an object that would be only available using the extension 
* 
* cons: 
* -does not enforce one-o-one relation between renderer and session 
* -the burden of maintaining the module leaved to the user 
* -the burden of creating the module leaved to the user 
* -fixed StreamingModule implementation forever 
* */ 
public void createSessionWithUserInstalledModule(VideoRenderer.Callbacks renderer) throws Exception { 
    SessionFactory factory = SessionFactory.newInstance(); 
    StreamingModule module = new StreamingModule(); 
    module.useRenderer(renderer); 
    factory.install(module); 
    factory.create(context, sessionCreatedCallback); 

} 

方法B:

/** 
* A static field of the module holds the instance. 
* The module will be implicitly picked up, and instantiated 
* when it's on the classpath. 
* It will be injected into the session during construction time. 
* 
* pros: 
* -module doesn't need to be maintained by user 
* -trivial implementation from user side 
* 
* cons: 
* -only one renderer is possible 
* -only one renderer will be available to all session instances 
* -possibility of leaking the renderer instance 
* */ 
public void createSessionWithStaticHolder(VideoRenderer.Callbacks renderer) throws Exception { 
    SessionFactory factory = SessionFactory.newInstance(); 
    StreamingModule.setRenderer(renderer); 
    factory.create(context, sessionCreatedCallback); 
} 

方法C:私はLATTを選ぶだろう

/** 
* The module can be retrieved from the session instance, after the 
* session is created. 
* The module will be implicitly picked up, and instantiated 
* when it's on the classpath. 
* It will be injected into the session during construction time. 
* 
* pros: 
* -StreamingModule implementation can be replaced in the future 
* -session instances have their own module 
* -only the session instance needed to be maintained (which is probably 
* already done on the user side) 
* 
* cons: 
* -less trivial implementation on user side 
* */ 
public void createSessionUsingServiceStyle(final VideoRenderer.Callbacks renderer) throws Exception { 
    SessionFactory factory = SessionFactory.newInstance(); 
    factory.create(context, new SessionFactory.SessionCreationCallback() { 
     @Override 
     public void onSessionCreated(Session session) { 
      StreamingModule module = session.getModule(StreamingModule.class); 
      module.useRenderer(renderer); 
     } 
    }); 
} 

私はそれを使いやすさと将来のスケーラビリティとの間のゴールデンパスと見なしています。私のコメントを見て、助言してください!

+0

これはあなたのAPIのうち、どの部分がクライアントコードですか? –

+0

メソッドはクライアントコードを表します。 _SessionFactory、StreamingModule_のクラスおよび関連するメソッドはAPIになります。 –

答えて

1

私は、使用するのが簡単で、「負担」の中間の名前が「柔軟性」であるため、ほとんどAを選択しました。

まだ、モジュールを取得するためにクラスパスに魔法がないAとCの息子と一緒に行きます。

思考/推論: 同じ文章で「JVM」、「暗黙」、「クラスパス」という言葉が聞こえるたびに、私はバスにぶつかっているような髪を失います。

非同期SessionCreationCallbackをシーケンシャルコードと比較するのが難しいユーザーもいますが、これは実際にはコンピュータに関するRTFMのケースです。

関連する問題