Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting many 'if else' statements to a cleaner approach [duplicate]

My code here detects if the mimeType is equals to some MIME type, if it is, it will do a certain conversion

public void convertToMp3(File src, File target,String mimeType){     if(mimeType.equals("audio/mpeg")){         ...     }else if(mimeType.equals("audio/wav")){         mp3ToWav();     }else if(mimeType.equals("audio/ogg")){         ...     }else if(...){     ... //More if and else here } 

I have shortened my code, because it has a lot of else if statements, What design pattern is suitable for removing many if and else or else if statements?

like image 887
user962206 Avatar asked Jan 03 '13 10:01

user962206


People also ask

How do you convert if else statements?

Place your cursor in the if keyword. Press Ctrl+. to trigger the Quick Actions and Refactorings menu. Select from the following two options: Select Convert to 'switch' statement.


2 Answers

You could have a Converter interface. Then you could create a class for each Mimetype like:

public interface Converter {      public void convertToMp3();     public void convertToOgg();  }  public class MpegConverter implements Converter {      public void convertToMp3() {         //Code here     }      public void convertToOgg() {         //Code here     }  } 

You would need a class like this for each converter. Then you could set up a map like this:

Map<String, Converter> mimeTypeMap = new HashMap<String, Converter>();  mimeTypeMap.put("audio/mpeg", new MpegConverter()); 

Then your convertToMp3 method becomes like this:

Converter converter = mimeTypeMap.get(mimeType); converter.convertToMp3(); 

Using this approach you could easily add different converters in the future.

All untested, probably doesn't compile, but you get the idea

like image 86
cowls Avatar answered Oct 02 '22 06:10

cowls


If you use pre-JDK7, you may add an enum for all MIME types:

  public static enum MimeTypes {       MP3, WAV, OGG   }    public class Stuff {       ...       switch (MimeTypes.valueOf(mimeType)) {           case MP3: handleMP3(); break;           case WAV: handleWAV(); break;           case OGG: handleOGG(); break;       }   } 

And have a look at the Stack Overflow question Java - Convert String to enum on how to convert Strings to enums.

like image 20
pgras Avatar answered Oct 02 '22 06:10

pgras