Sharad Yadav
Sharad Yadav

Reputation: 3061

What is the best/standard way to define a web service interface (inheritance/overloading)?

What is the best/standard way to define a web service interface?

For example,

I have two class Car and Bus both of them extends Vehicle.

If I want to expose a create method for Car and Bus I have following options -

  1. public void create(Vehicle v);
  2. public void create(String type, Vehicle v);
  3. public void create(Car c); and public void create(Bus b);
  4. public void createCar(Vehicle v); and public void createBus(Vehicle v);
  5. public void createCar(Car c); and public void createBus(Bus b);

EDIT---------------------------------

My main concern is of the above 5 options what is the standard way for a web service API. What is standard for java coding may not be standard for a webservice.

Upvotes: 1

Views: 404

Answers (3)

AlexR
AlexR

Reputation: 115378

First of all your methods are called create and therefore by convention should be a kind of factory methods, therefore shouild return instance of Vehicle. Your methods look more like init since they receive instance of Vehicle, Car, Bus.

So, I'd suggest you to modify your signatures like

Vehicle create(*some arguments*)

Now about the reurn type. I'd suggest you to use generics based definition like

<V extends Vehicle> V create()

This method may be called without casting to specific Vehicle subclass.

About the arguemtns. I believe that you can use

  1. String type (as you suggested)
  2. Class type
  3. Define special enum Vehicles {Car, Bus, Bike} and use it.

Alternatively you can (as you suggested) create several factory methods for each type of Vehicle like createCar and createBus but I dislike this method because every time you add new type of Vehicle you have to add such method.

Upvotes: 1

Cratylus
Cratylus

Reputation: 54094

If you are interested in the standard way then none of those is.
Web services do not support operation overloading and it is prohibited in WS BP Profile
WS BP

4.5.3 Distinctive Operations

Operation name overloading in a wsdl:portType is disallowed by the Profile. 

This actually makes sense since the message mode (doc/lit) expects the operation name as the root of the message payload to do the dispatching.
Additionally, you are talking about OO concepts but web services standards are meant as an integration technology (not bind to specific languages or platforms).
In any case if you are expected to do that, you can implement overloading via tweeking at annotations and in your case, IMHO I think the best option would be 2 so that the consumer knows how to downcast it.

Upvotes: 2

RMT
RMT

Reputation: 7070

The first one seems like the most generic one that you can use, and you just override it inside your Car or Bus class

Upvotes: 2

Related Questions