0

I have a method which is implemented as below

public void setAttributes(Data d) {
  Model m;
  String type = d.getType();
  if (type.equals("TYPE1")) {
    m.setType(type);
    m.setDuration(d.getDuration())
    m.setBenefit(d.getBenefit())
    m.setPermission(d.getPermission());
  } else if (type.equals("TYPE2")) { /* Here we wont be having duration, benefit */
    m.setType(type);
    m.setLab(true)
  }
}

This doesnot look scalable because in future if some more TYPES comes, then we need to keep adding if else blocks. Is there any way I can refactor this. Note: Attributes will be different based on types.

Sunil
  • 856
  • 12
  • 24
  • Using a `switch` construct here seems like a better option provided you plan on adding more options. – developer10 Sep 14 '16 at 08:51
  • You can change type to enum and make switch from if-else statements. Or you can check *visitor* pattern. – ByeBye Sep 14 '16 at 08:51

4 Answers4

2

You can make a subclass of Data per each type and use overriding to attach the behavior to each class individually. This makes sense if you plan to have very many data types and a lot of associated logic.

public abstract class Data {
    ...
    public abstract void setAttributes(Model m);
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Consider defining an enum for possible types.

Change the type of getType() to that enum.

Convert your if block to a switch block that switches on that enum.

Using a String is a weaker implementation technique since it's not quite so type-safe if you get my meaning.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    How does this scale? Now you keep adding case statements? I wanted a solution where we can define a config/behavior in map and then use it instead of if and else – Sunil Sep 14 '16 at 08:55
  • A jump to an `enum` label in a `switch` is O(1). So it scales. Maintenance is no more than adding a new `enum` type and a corresponding `case`. But do consider @MarkoTopolnik approach though. – Bathsheba Sep 14 '16 at 08:59
0

You can use switch

switch(type) {
    case "TYPE1":
        m.setType(type);
        m.setDuration(d.getDuration())
        m.setBenefit(d.getBenefit())
        m.setPermission(d.getPermission());
        break;
    case "TYPE2":
       m.setType(type);
       m.setLab(true)
       break;
}

This will provide more scalable template.

Guy
  • 46,488
  • 10
  • 44
  • 88
0

Maybe the switch-case statement is suiting for you (see: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html)

This means for your code example:

public void setAttributes(Data d) {
   Model m;
   String type = d.getType();
   switch(type) {
       case "Type1":
           m.setType(type);
           m.setDuration(d.getDuration())
           m.setBenefit(d.getBenefit())
           m.setPermission(d.getPermission());
           break;
       case "TYPE2": 
          m.setType(type);
          m.setLab(true);
          break;
  }
}

Hope this helps

DMuenstermann
  • 31
  • 1
  • 3