私は基本的な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);
}
}
ありがとう!
いくつかの考え:a)SOはcodereviewサイトではありません(codereview.stackexchange.comは1つです)。B)あなたはインターフェイスについて話していますが、私が見るのはクラスだけです。ここで重要なのは、インタフェースは具体的なクラスではなく、インタフェースでなければならないということです。 C)それを超えて:ビジネス論理オブジェクトのための**新しい**はありません。クラスへのオブジェクトの取得には依存関係注入を絶対に使用する必要があります。短いストーリー:https://www.youtube.com/playlist?list=PLD0011D00849E1B79 ...すべてを見てください。 – GhostCat
質問者がコードレビューのようなフィードバックを求めているので、私はこのトピックを議論の対象外としています。 – GhostCat
この@GhostCatのフィードバックに感謝します。私はcodereviewでこれを投稿する必要があることに同意します..それについて申し訳ありません! –