2016-07-20 22 views
0

私は基本的なJavaを知っていますが、私はときどきオブジェクト指向設計に苦しんでいます。Javaデザイン:ベンダーAPIを公開してカプセル化する方法

私は使用しているベンダーのAPIがあります。他のプロジェクトでは再利用できるようにラップしたいと思っていました。lib ベンダーのすべてのサービスは異なるクラスであり、階層などはありませんが、変更するオプションはありません。 私は作曲を使い、自分自身を繰り返さないようにしたいと思います。

私は当初、すべてのサービスに共通のパラメータを受け取るサービスを作成すると考えていました。このサービスはapiを実装します。

私はここにあるこのコードをリファクタリングしようとした、と私は私がしようとしている。この設計は、ユニットテストを作成しようとしたとき、私は気づいたとしていくつかの大きな問題を抱えている:)

どのように私はより良いを達成できるかなり確信しています設計?今のよう コード:

/* This is how I call the service from the vendor today */ 
class VendorConsumer { 
    void exampleCake() { 
     VendorServiceCake vendorServiceCake = new VendorServiceCake(); 
     VendorApiCake cakeApi = a.getCakeApi(1234); 
     cakeApi.authenticate("user", "password"); 
     cakeApi.cookDeliciousCake(CakeIngredients ingredients); 
    } 

    void exampleSellPie() { 
     VendorServiceSellPie vendorServiceSellPie = new VendorServiceSellPie(); 
     VendorApiSellPie apiSellPie = a.getPieApi(1234); //same parameters as above 
     apiSellPie.authenticate("user", "password"); //same parameters as above 
     apiSellPie.sellDeliciousPie(List<Customer> customer); 
    } 
} 

// --------------------------------------------- 
/* Below is what I'm trying to do */ 
class UsageTest { 
// This is how users of my .jar would call it 
    void usage() { 
     BakeryService service = new BakeryServiceCake("user", "password", 1234); 
     List<Cake> cakeList = service.cookDeliciousCake(CakeIngredients ingrediets); 
    } 
    void usage2() { 
     BakeryService service = new BakeryServiceSellPie("user", "password", 1234); 
     List<Payments> payments = service.sellDeliciousPie(List<Customer> customer); 
    } 
} 

class BakeryService { //is this class useless? 
    public BakeryService(String user, String pass, int parameterNeeded) { 
    } 
    private void checkParameters() { 
     //do some checkings of the parameters 
    } 
} 

class BakeryServiceCake extends BakeryService implements KitchenCakeApi { 
    KitchenCakeApi api; 
    public BakeryServiceCake(String user, String pass, int parameterNeeded) { 
     super(user, pass, parameterNeeded); 
     this.api = new KitchenCakeApiImpl(user, pass, parameterNeeded) 
    } 

    @Override 
    public void authenticate() { 
     api.authenticate(); 
    } 

    @Override 
    public void cookDeliciousCake(CakeIngredients ingredients) { 
     api.cookDeliciousCake(ingredients); 
    } 
} 

interface KitchenCakeApi { 
    void authenticate(); 
    void cookDeliciousCake(CakeIngredients ingredients); 
} 

class KitchenCakeApiImpl implements KitchenCakeApi { 

    private VendorServiceCake vendorServiceCake; 
    private VendorApiCake cakeApi; 

    public KitchenCakeApiImpl(String user, String pass, int parameterNeeded) { 
     vendorServiceCake = new VendorServiceCake(); 
     cakeApi = a.getCakeApi(parameterNeeded); // that 1234 
    } 

    @Override 
    public void authenticate() { 
     cakeApi.authenticate("user", "password"); 
    } 

    @Override 
    public void cookDeliciousCake(CakeIngredients ingredients) { 
     cakeApi.cookDeliciousCake(CakeIngredients ingredients); 
    } 
} 

ありがとう!

+0

いくつかの考え:a)SOはcodereviewサイトではありません(codereview.stackexchange.comは1つです)。B)あなたはインターフェイスについて話していますが、私が見るのはクラスだけです。ここで重要なのは、インタフェースは具体的なクラスではなく、インタフェースでなければならないということです。 C)それを超えて:ビジネス論理オブジェクトのための**新しい**はありません。クラスへのオブジェクトの取得には依存関係注入を絶対に使用する必要があります。短いストーリー:https://www.youtube.com/playlist?list=PLD0011D00849E1B79 ...すべてを見てください。 – GhostCat

+2

質問者がコードレビューのようなフィードバックを求めているので、私はこのトピックを議論の対象外としています。 – GhostCat

+0

この@GhostCatのフィードバックに感謝します。私はcodereviewでこれを投稿する必要があることに同意します..それについて申し訳ありません! –

答えて

0

APIには一般的にいくつかの階層構造がありますが、Javaファイルの処理はあまりにも正確なテキストブックの実装に基づくデコレータパターンに基づいています。

ベンダーAPIを見ずに答えるのは難しいです。 考えられるアプローチ -

  1. アダプターパターンヘルプを使用すると、サードパーティAPIからカプセル化することができます。 アダプタには書き込むコードがたくさんありますが、保守は簡単です。
  2. アダプターが爆発する場合は、プロキシパターンを使用して、ライフビットを簡単にします。
  3. ファサードは複雑な操作の場合に役立ちますが、これはあなたの場合のアドオンに過ぎません。

二セントからhttp://www.oodesign.com/はちょうど概要とデザインパターンを通過し、それがベルを鳴らした場合、より深い他に行く上で動く解決の問題を参照してください。

ハッピーデザイン。

関連する問題