user3279506
user3279506

Reputation: 21

How can I implement different return type based on the cases in switch case

I have written a method based that returns different types based on the result of a switch condition. I am not able to get different return type based on the cases.

public Object callCommonMethod(char key, Customer customer)
        throws SFWException, ServiceException, CloneNotSupportedException {

    switch (key) {

    case 'C':
         performGetCDBProfile(customer.getWizardNum());
         dbData=true;
         break;

    case 'U':
        performUpdate(cdbProfileInfo);
        break;

    case 'D':
        updateCDBProfile(cdbProfileInfo);
        break;

    case 'W':
        updateWebProfile(customer);
        break;

    case 'M':
         ObjectMappingService.mapInfoToEDO(cdbProfileInfo);
        break;
    }
    return customer;
}

How can I implement that performGetCDBProfile(customer.getWizardNum()); returns cdbProfileInfo obj, performUpdate(cdbProfileInfo); and updateCDBProfile(cdbProfileInfo); are methods that don't have return type, and updateWebProfile(customer); returns customer object.

Upvotes: 0

Views: 1223

Answers (3)

user3701861
user3701861

Reputation: 151

  1. It is a bad practice to return Object and check its instance in the calling method.
  2. Even then if you still want to achieve your goal, Why cant you return null in those 2 cases where the method doesnt have anything to return? (not recommeneded)

Upvotes: 1

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34224

If you have a method and it looks like it should return objects of different entities, types - then something's definitely wrong with your architecture.
Most probably, your method breaks the single responsibility principle.

Do not implement interface with the common method or create any other hacks. Change the logic of invocation. Remember: Abstractions should not depend upon details.

Split your method up. Why would someone return Customer object or cdbProfileInfo in a single method?
If you are going to call this method from a single place with different keys, then how are you going to work with results of different types? If you are going to call this method with different keys from different places - then, you can simply split this method up.

Upvotes: 1

Brian Agnew
Brian Agnew

Reputation: 272337

A method can only be declared to return one type. You can return different subtypes, but the compiler will only know at compile time that you're returning your declared base class.

Normally this isn't a problem if your subtypes are strongly related and your calling code can act on either type without knowing what your actual type is (this is the essence of polymorphism) e.g.

Vehicle v = getBusOrCar();
v.drive();

If you can't do something useful like the above (e.g. you're returning an Object since that's the most common base type), then that to me is a code smell. Is your method doing two different things ?

If you want to optionally return something or nothing, you can always return your object (a Customer type?) or null, check for the null upon receipt/prior to usage. Another possiblity is to return an Optional container, and consequently the API indicates that you may get an object back.

Either way, it still seems to me that perhaps your method has a variety of responsibilities. Perhaps your switch statement should delegate more functionality to another method/class for each case, and so you wouldn't need to return an object and act on it then ?

Upvotes: 1

Related Questions