如何最小化这种开关情况?

时间:2017-04-20 09:24:05

标签: java

我通过服务电话获得了不同的提供商。

在那个基础上我的标签部分会有所不同;我想最小化这段代码:

if(selectedProvider.equalsIgnoreCase("youtube")){
    switch (tabName.toLowerCase()) {
        case "songs":
            sectionTab = "video";
            break;
        case "artists":
            sectionTab="";
            break;
        case "albums":
            sectionTab="channel";
            break;
        case "playlists":
            sectionTab="playlist";
            break;
    }}
else if(selectedProvider.equalsIgnoreCase("soundcloud")){
    switch (tabName.toLowerCase()) {
        case "songs":
            sectionTab = "track";
            break;
        case "artists":
            sectionTab="artist";
            break;
        case "albums":
            sectionTab="";
            break;
        case "playlists":
            sectionTab="playlist";
            break;
    }}
else {
    switch (tabName.toLowerCase()) {
        case "songs":
            sectionTab = "track";
            break;
        case "artists":
            sectionTab = "artist";
            break;
        case "albums":
            sectionTab = "album";
            break;
        case "playlists":
            sectionTab = "playlist";
            break;
    }
}

4 个答案:

答案 0 :(得分:1)

这里有一个可能的解决方案:使用包含地图的地图。

像:

Map<String, String> soundCloudMappings = new HashMap<>();
soundCloudMappings.put("songs", "track");

...

Map<String, Map<String, String> providerMappings = ...
providerMappings.put("soundcloud", soundCloudMappings);

然后你可以检查你的外部地图中是否存在provider.toLowerCase();然后向内部地图询问正确的sectionTab条目。

但当然,这是一个非常“低级”的解决方案。根据您的上下文,您可能更愿意将这些原始字符串转换为Enums常​​量;并为该枚举添加漂亮的映射方法。换句话说:考虑平衡灵活性(用字符串做所有事情)和更高的编译时间安全性。

答案 1 :(得分:0)

您可以使用这样的几个地图进行翻译

static final Map<String, String> SECTION_TAB2 = new LinkedHashMap<>();
static final Map<String, String> SECTION_TAB1 = new LinkedHashMap<>();

static {
    // are there special two word outcomes
    SECTION_TAB2.put("youtube songs", "artists");
    SECTION_TAB2.put("youtube artists", "");
    SECTION_TAB2.put("youtube albums", "channel");
    SECTION_TAB2.put("soundcloud albums", "");
    // if not, what are the default tab name outcomes.
    SECTION_TAB1.put("songs", "track");
    SECTION_TAB1.put("artists", "artist");
    SECTION_TAB1.put("albums", "album");
    SECTION_TAB1.put("playlists", "playlist");
}

public static String sectionTab(String selectedProvider, String tabName) {
    return SECTION_TAB2.getOrDefault((selectedProvider + " " + tabName).toLowerCase(),
            SECTION_TAB1.get(tabName.toLowerCase()));
}

答案 2 :(得分:0)

我无法发表评论所以我正在评论

的答案
      public static void main(String[] args) throws IOException
{
    Table<String,String,String> table= HashBasedTable.create();
    table.put("youtube","songs","video");
    table.put("youtube","artists","");
    table.put("youtube","albums","channel");
    table.put("youtube","playlists","playlist");
    table.put("soundcloud","songs","track");
    table.put("soundcloud","artists","artist");
    table.put("soundcloud","albums","");
    table.put("soundcloud","playlists","playlist");
    table.put("default","songs","track");
    table.put("default","artists","artist");
    table.put("default","albums","albums");
    table.put("default","playlists","playlist");
  String sectionTab =  table.get("soundcloud","artists"); // u will get artist
}

我们可以使用guava表来避免地图的地图,并且易于维护和修改

https://www.tutorialspoint.com/guava/guava_table.htm

答案 3 :(得分:0)

我更喜欢构建结构来定义逻辑,而不是在这样的情况下对其进行编码。

// What tabs we have.
enum Tabs {
    songs,
    artists,
    albums,
    playlists;
    // Build a lookup.
    static Map<String, Tabs> lookup = Arrays.stream(Tabs.values()).collect(Collectors.toMap(e -> e.name(), e -> e));

    static Tabs lookup(String s) {
        return lookup.get(s);
    }
}

// The providers.
enum Providers {
    youtube("video","","channel","playlist"),
    soundcloud("track","artist","","playlist"),
    others("track","artist","album","playlist");
    // Build a String lookup.
    static Map<String, Providers> lookup = Arrays.stream(Providers.values()).collect(Collectors.toMap(e -> e.name(), e -> e));

    Map<Tabs,String> tabs = new HashMap<>();
    Providers(String track, String artist, String album, String playlists) {
        tabs.put(Tabs.songs, track);
        tabs.put(Tabs.artists, artist);
        tabs.put(Tabs.albums, album);
        tabs.put(Tabs.playlists, playlists);
    }

    static Providers lookup(String s) {
        Providers p = lookup.get(s);
        // Default to others.
        return p == null ? others : p;
    }

    public static String getSectionTabName(String provider, String tabName) {
        // Lookup the provider.
        Providers p = lookup(provider);
        Tabs t = Tabs.lookup(tabName);
        return p.tabs.get(t);
    }
}

public void test() {
    String provider = "youtube";
    String tabName = "albums";
    String section = Providers.getSectionTabName(provider, tabName);
    System.out.println(provider+"!"+tabName+" = "+section);
}

这样做的好处:

  • 添加新的提供商只需添加新的枚举。
  • 添加新标签稍微不那么简单(向Providers构造函数添加新参数)但仍然不会显着更改代码。