模版模式构建可复用性大集合切片循环,使用java8 lambda去调用可以更加简洁哦
2015-05-05 17:12
597 查看
面向过程的编程思路相信大家都不陌生,随着脚本语言的盛行,面向过程的编程开始火起来了,面向过程并不是说我的代码就不面向对象了,可以把通用性的地方面向对象的思路封装,局部可复用性少的代码面向过程去编写组装。
相信用过google gauva的程序员对Lists.transfer并不陌生,这样的面向过程的语法在java8后可以非常的美丽。
其实咱们在工作中有很多过程是可抽象剥离,往往因为局部业务不同造成相当多的代码重复,这时候应该采用面向过程编程去解决这个问题。
下面这块代码在开发过程中如果不采用一定的设计模式 repeat code R会出现在我们的系统各个角落,于是乎采用模版模式来剥离变化的地方。
本文出自 “程序猿De香蕉” 博客,请务必保留此出处http://qiyubin.blog.51cto.com/3642349/1642185
相信用过google gauva的程序员对Lists.transfer并不陌生,这样的面向过程的语法在java8后可以非常的美丽。
其实咱们在工作中有很多过程是可抽象剥离,往往因为局部业务不同造成相当多的代码重复,这时候应该采用面向过程编程去解决这个问题。
下面这块代码在开发过程中如果不采用一定的设计模式 repeat code R会出现在我们的系统各个角落,于是乎采用模版模式来剥离变化的地方。
package com.qunar.piao.sight.common.util; import com.google.common.annotations.GwtCompatible; import org.apache.xmlbeans.impl.xb.xsdschema.Public; import java.util.Collection; import java.util.List; /** * Created by yubin.qi on 2015/4/15. */ public class SubListUtil { //Function<? super F, ? extends T> public static interface SubListFuntion<T> { public void processor(List<T> tList); } public static <T> void dealForSubList(List<T> totalList, Integer subSize, SubListFuntion<T> subFuntion) { //repeat code R--START int count = totalList.size(); int iteratorCount = count / subSize; for (int i = 0; i <= iteratorCount; i++) { int endIndex, startIndex; startIndex = i * subSize; endIndex = ((endIndex = (i + 1) * subSize) > count) ? count : endIndex; if (endIndex == startIndex) { break; } List<T> subList = totalList.subList(startIndex, endIndex); //---A bussiness code start subFuntion.processor(subList); //---A bussiness code end } //repeat code R--END } public static void main(String args[]) { List<String> as = null; SubListUtil.dealForSubList(as, 1000, new SubListFuntion<String>() { @Override public void processor(List<String> strings) { //you want write code! or you defined method! } }); //java8 SubListUtil.dealForSubList(as, 1000, (strings)->{ //you want write code! or you defined method! })); } }
本文出自 “程序猿De香蕉” 博客,请务必保留此出处http://qiyubin.blog.51cto.com/3642349/1642185
相关文章推荐
- 为什么 Kotlin 调用 java 时可以使用 Lambda? —— SAM 转换机制的介绍
- java8——使用lambda和枚举实现简洁、可读性良好的策略模式
- java8实战七:使用Lambda重构面向对象的设计模式
- java中使用Builder模式构建多个参数的构造器
- 使用WebElements定位多个相似的元素,比如页面中存在五个单选按钮,他们有相同的class属性,值为:myRadio,我们想对五个按钮循环操作,我们可以把它们全部取出来放到集合中,然后做循环操作
- Java1.8新特性关于lambda表达式与Stream流的使用以及对集合的实用操作
- 五星-原型模式常使用于以下场景--而JAVA中的任何类只要实现了Cloneable标识接口,就可以使用clone方法来进行对象的拷贝
- Java使用设计模式中的代理模式构建项目的实例展示
- 使用开放源代码框架的 Java 应用程序的 Web 服务集成模式,第 1 部分: 实现调用模式
- 在ASPX页面中使用include命令的发现,在DIV中显示另一页面的内容,这样可以使页面更加简洁
- Mainsoft的Grasshopper让你可以使用C#编写Java可以调用的类库(免费了!)
- IEnumerable 接口---实现该接口里的成员的类,可以使用foreach循环访问集合
- 在终端模式下可以使用curses函数库。 在图形界面下Gnome系统可以使用Gtk, KDE系统可是使用Qt. 最保险的是用Java, 跨平台
- 使用OpenLayers以切片方式调用mapxtreme java渲染出的地图遇到的问题
- 黑马程序员_Java基础_面向对象(Static的使用、对象初始化和调用成员过程、单例设计模式)
- 使用JAVA远程调用导致程序自动down掉,jdk1.5的debug模式系统漏洞
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与LinkedHashSet类))
- java 连接orcale (使用for循环,循环次数不可以超过299)一
- 【Java编程】使用增强for循环和迭代器遍历Map集合
- [Java 8] (2) Lambda在集合中的使用