wordpress 登陆窗口,优化关键词是什么意思,wordpress充值中心,深圳网站制作服转载自 避免代码冗余#xff0c;使用接口和泛型重构Java代码在使用动态语言和.NET工作了若干年后#xff0c;我又回到老本行–Java开发。在Ruby中#xff0c;清除代码冗余是非常方便的#xff0c;而在Java中则需要结合接口和泛型实现类似的功能。
原始代码
以下是这个类中的…转载自 避免代码冗余使用接口和泛型重构Java代码在使用动态语言和.NET工作了若干年后我又回到老本行–Java开发。在Ruby中清除代码冗余是非常方便的而在Java中则需要结合接口和泛型实现类似的功能。
原始代码
以下是这个类中的一些方法用于后续的阐述。为了使例子更简洁我移除了些代码。
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667publicV get(finalK key){ Session s; try{ s oGrid.getSession(); ObjectMap map s.getMap(cacheName); return(V) map.get(key); } catch(ObjectGridException oge) { thrownew RuntimeException(Error performing cache operation, oge); } finally { if(s ! null) s.close(); } returnnull;} publicvoid put(finalK key, finalV value){ Session s; try{ s oGrid.getSession(); ObjectMap map s.getMap(cacheName); map.upsert(key, value); } catch(ObjectGridException oge) { thrownew RuntimeException(Error performing cache operation, oge); } finally { if(s ! null) s.close(); } }publicMapK, V getAll(Set? extendsK keys){ finalListV valueList newArrayListV(); finalListK keyList newArrayListK(); keyList.addAll(keys); Session s; try{ s oGrid.getSession(); ObjectMap map s.getMap(cacheName); valueList.addAll(map.getAll(keyList)); } catch(ObjectGridException oge) { thrownew RuntimeException(Error performing cache operation, oge); } finally { if(s ! null) s.close(); } MapK, V map newHashMapK, V(); for(inti 0; i keyList.size(); i) { map.put(keyList.get(i), valueList.get(i)); } returnmap;}遇到的问题
123456789101112131415Session s;try{ s oGrid.getSession(); ObjectMap map s.getMap(cacheName); // Some small bit of business logic goes here}catch(ObjectGridException oge){ thrownew RuntimeException(Error performing cache operation, oge);}finally{ if(s ! null) s.close(); }上面的代码段几乎存在于类的每个方法中这违反了DRY原则 。将来如果需要改变检索Session 和 ObjectMap实例的方式或着某天这段代码被发现有缺陷我们就不得不修改每个(包含这段代码的)方法因此需要找到一种方式来复用这些执行代码。
重构后的代码
为了传递包含了原方法中业务逻辑的实例我们创建一个带有抽象方法的 Executable 接口 。execute()方法参数为我们欲操作的ObjectMap实例。
123interfaceExecutableT { publicT execute(ObjectMap map) throwsObjectGridException;}由于我们的目的仅仅是在每个方法中操作ObjectMap实例可以创建executeWithMap()方法封装前述的那一大段重复代码。这个方法的参数是Executable接口的实例实例包含着操作map的必要逻辑(译者注这样Executable接口的实例中就是纯粹的业务逻辑实现了解耦合)。
12345678910111213141516171819privateT T executeWithMap(ExecutableT ex){ Session s; try{ s oGrid.getSession(); ObjectMap map s.getMap(cacheName); // Execute our business logic returnex.execute(map); } catch(ObjectGridException oge) { thrownew RuntimeException(Error performing cache operation, oge); } finally { if(s ! null) s.close(); }}现在可以用如下形式的模板代码替换掉第一个例子中的代码这个模板创建了一个匿名内部类实现了Executable接口和execute()方法。其中execute()方法执行业务逻辑并以getXXX()的方式返回结果(若为Void方法返回null)
1234567891011121314151617181920212223242526272829303132333435363738publicV get(finalK key){ returnexecuteWithMap(newExecutableV() { publicV execute(ObjectMap map) throwsObjectGridException { return(V) map.get(key); } }); } publicvoid put(finalK key, finalV value){ executeWithMap(newExecutableVoid() { publicVoid execute(ObjectMap map) throwsObjectGridException { map.upsert(key, value); returnnull; } }); }publicMapK, V getAll(Set? extendsK keys){ finalListK keyList newArrayListK(); keyList.addAll(keys); ListV valueList executeWithMap(newExecutableListV() { publicListV execute(ObjectMap map) throwsObjectGridException { returnmap.getAll(keyList); } }); MapK, V map newHashMapK, V(); for(inti 0; i keyList.size(); i) { map.put(keyList.get(i), valueList.get(i)); } returnmap;}FunctionalInterface Annotation (功能接口注释)
Java 8 的 FunctionalInterface annotation 使这一切变的简单。若某接口带有一个抽象方法这个接口便可以被用作为lambda表达式的参数称为功能接口。
1234FunctionalInterfaceinterfaceExecutableT { publicT execute(ObjectMap map) throwsObjectGridException;}只要接口仅仅包含一个抽象方法便可以使用这个annotation。这样就能减少相当数量的模板代码。
12345678910111213141516171819202122publicV get(finalK key){ returnexecuteWithMap((ObjectMap map) - (V) map.get(key));} publicvoid put(finalK key, finalV value){ executeWithMap((ObjectMap map) - { map.upsert(key, value); returnnull; });}publicMapK, V getAll(Set? extendsK keys){ finalListK keyList newArrayListK(); keyList.addAll(keys); ListV valueList executeWithMap((ObjectMap map) - map.getAll(keyList)); MapK, V map newHashMapK, V(); for(inti 0; i keyList.size(); i) { map.put(keyList.get(i), valueList.get(i)); } returnmap;}结论
实现这些重构我很开心。它比原始的代码略复杂一点但是更简明更DRY所以一切都是值得的。 尽管还有提升的空间但这是一个良好的开始。
原文链接
michaelbrameld翻译
ImportNew.com
-
ImportNew读者译文链接
http://www.importnew.com/6761.html