
这两天再基于 ShareSDK Cocos2d-x 的 Sample 代码做 Lua 的分享接口。
有些 java 代码不得不来吐槽一下,顺便给出我的重构代码,请大家帮忙指正下:
if (!(content.get("title") == null)) { map.put("title", content.get("title")); } if (!(content.get("description") == null)) { map.put("comment", content.get("description")); } if (!(content.get("url") == null)) { map.put("url", content.get("url")); map.put("titleUrl", content.get("url")); } if (!(content.get("site") == null)) { map.put("site", content.get("site")); } if (!(content.get("siteUrl") == null)) { map.put("siteUrl", content.get("siteUrl")); } if (!(content.get("musicUrl") == null)) { map.put("musicUrl", content.get("musicUrl")); } if (!(content.get("extInfo") == null)) { map.put("extInfo", content.get("extInfo")); } 碍眼的地方:
private static final Map<String, String> NATIVE_FIELDS; static { Map<String, String> map = new HashMap<String, String>(); map.put("text", "content"); map.put("title", "title"); map.put("comment", "description"); map.put("url", "url"); map.put("titleUrl", "url"); map.put("site", "site"); map.put("siteUrl", "siteUrl"); map.put("musicUrl", "musicUrl"); map.put("extInfo", "extInfo"); NATIVE_FIELDS = Collections.unmodifiableMap(map); } for (Map.Entry<String, String> entry : NATIVE_FIELDS.entrySet()) { String nativeName = entry.getValue(); String wantedName = entry.getKey(); Object value = content.get(nativeName); if (value != null) { map.put(wantedName, value); } } if (map.containsKey("text")) { oks.setText(String.valueOf(map.get("text"))); } if (map.containsKey("imagePath")) { oks.setImagePath(String.valueOf(map.get("imagePath"))); } if (map.containsKey("imageUrl")) { oks.setImageUrl(String.valueOf(map.get("imageUrl"))); } if (map.containsKey("title")) { oks.setTitle(String.valueOf(map.get("title"))); } if (map.containsKey("comment")) { oks.setComment(String.valueOf(map.get("comment"))); } if (map.containsKey("url")) { oks.setUrl(String.valueOf(map.get("url"))); } if (map.containsKey("titleUrl")) { oks.setTitleUrl(String.valueOf(map.get("titleUrl"))); } if (map.containsKey("site")) { oks.setSite(String.valueOf(map.get("site"))); } if (map.containsKey("siteUrl")) { oks.setSiteUrl(String.valueOf(map.get("siteUrl"))); } 比片段一难一点,但是在一个习惯了 Lua 的 字段就是字符的人眼里,还是看着碍眼。
Java 不是有反射吗?当然反射的代码有点麻烦,还要异常处理。 Android 的 Java 是不是 Java 8 哦?
private static final Map<String, Method> OKS_FIELDS; private static Method getSetter(String name) throws NoSuchMethodException { return OnekeyShare.class.getMethod(name, String.class); } static { Map<String, Method> map = new HashMap<String, Method>(); try { map.put("comment", getSetter("setComment")); map.put("imagePath", getSetter("setImagePath")); map.put("imageUrl", getSetter("setImageUrl")); map.put("site", getSetter("setSite")); map.put("siteUrl", getSetter("setSiteUrl")); map.put("text", getSetter("setText")); map.put("title", getSetter("setTitle")); map.put("titleUrl", getSetter("setTitleUrl")); map.put("url", getSetter("setUrl")); } catch (NoSuchMethodException e) { Log.e("share", "error", e); } OKS_FIELDS = Collections.unmodifiableMap(map); } try { for (Map.Entry<String, Method> entry : OKS_FIELDS.entrySet()) { String field = entry.getKey(); Method set = entry.getValue(); String value = (String)content.get(field); if (value != null) { set.invoke(oks, value); } } } catch (Exception e) { Log.e("share", "error", e); } 如果是 Lua ,会写成这样:
local NATIVE_FIELDS = { text = "content", title = "title", comment = "description", url = "url", titleUrl = "url", site = "site", siteUrl = "siteUrl", musicUrl = "musicUrl", extInfo = "extInfo", } for k, v in ipairs(NATIVE_FIELDS) do map[k] = content[v] end 和这样:
local OKS_FIELDS = { setComment = "comment", setImagePath = "imagePath", setImageUrl = "imageUrl", setSite = "site", setSiteUrl = "siteUrl", setText = "text", setTitle = "title", setTitleUrl = "titleUrl", setUrl = "url", } for k, v in ipairs(OKS_FIELDS) do oks[k](content[v]) end 1 ruixianxx 2015-11-08 15:46:56 +08:00 Sample 代码最重要的就是可读性啊。。 我倒是觉得写的简单粗暴挺好的 毕竟谁都能看得懂 |
3 liuxey 2015-11-09 09:31:24 +08:00 为什么我觉得 if else 更舒服。。。 |
5 coolxiao 2015-11-24 14:21:54 +08:00 应该是 pairs 不是 ipairs 吧 |