0

I am writing a client wrapper for a service. Which have given client implementation. Having Two class hierarchy:

  1. Account
  2. Product

Both classes doesn't share common parent interface. But they while using them they have similar implementation:

Example of piece of code performing

Account service you need following steps of code:

 final AccountsCustomBatchRequest content = new AccountsCustomBatchRequest();
 content.setEntries(request);
 final AccountsCustomBatchResponse response =   this.service.accounts().custombatch(content).setDryRun(this.isDryRun)
                        .execute();
return response.getEntries();

Similar for product:

final ProductsCustomBatchRequest content = new ProductsCustomBatchRequest();
 content.setEntries(request);
 final ProductsCustomBatchResponse response =   this.service.products().custombatch(content).setDryRun(this.isDryRun)
                        .execute();
return response.getEntries(); 

Is there a way to generalize the code? I can't use generic pattern as both those not support common parent class. Can I do like:

   T extends Account or Product ?
Vivek Goel
  • 22,942
  • 29
  • 114
  • 186
  • 2
    If they have no parent class why are you trying to generalize? If you're using the same methods you should be using a common interface, not a class. – Reut Sharabani Jul 30 '15 at 12:35
  • I would say that its generic enough. There is a point when making it more generic is bad, not saying you have reached that point yet but I would stop, personally. – Jakob Jul 30 '15 at 12:35
  • Your sample code doesn't show either `Account` or `Product` being used. – Paul Jul 30 '15 at 12:37
  • @ReutSharabani But while implementing wrapper. Business logic is nearly duplicated in account and product class. – Vivek Goel Jul 30 '15 at 12:37
  • Extract it in another class then. – biziclop Jul 30 '15 at 12:39
  • @Paul May be it was not clear enough. What I mean was both share similar class hierarchy. Account* and Product*. Example are ProductsCustomBatchRequest vs AccountsCustomBatchRequest. ProductsCustomBatchResponse vs AccountsCustomBatchResponse – Vivek Goel Jul 30 '15 at 12:39
  • @biziclop Can you please help me with a smaller example ? – Vivek Goel Jul 30 '15 at 12:41
  • Generate a utility class with the method you Need and pass the super class of both classes as parameters then cast by detecting the Parameter types using **instance of** – Kami Jul 30 '15 at 12:43
  • 2
    products and accounts should be passed over to batch processors (that could benefit from Generics). It's hard to make it generic now because you're doing it in the other way around, you're running the batch logic inside products and accounts – cahen Jul 30 '15 at 12:44
  • @cahen That is by implementation of third party lib. – Vivek Goel Jul 30 '15 at 12:46

2 Answers2

0

They share this method: custombatch(content)

I would create an interface containing the custombatch method and make something like T extends custombatchInterface

JFPicard
  • 5,029
  • 3
  • 19
  • 43
0

If both classes don't share the same hierarchy, then you can't combine them. A possibly way is to create a new Interface CustomBatchInterface with the method custombatch(content) and create two classes which extends the ProductsCustomBatchResponse and AccountsCustomBatchResponse but also implements your CustomBatchInterface.

Kami
  • 459
  • 1
  • 6
  • 27