Java_通过行为参数化传递代码
2017-12-18 14:40
435 查看
通过行为参数化传递代码
把一个行为(一段代码)封装起来,并通过传递和使用创建的行为(例如对Apple的不同谓词)将方法的行为参数化。
① 将筛选条件抽象化,作为参数进行传递
② 将输出信息抽象化,作为参数传递
③ 使用匿名类
④ 使用Lambda表达式
⑤ 将List类型抽象化
⑥ 例子:用Comparator 来排序
⑦ 例子:用Runnable 执行代码块
⑧ 例子:GUI 事件处理
把一个行为(一段代码)封装起来,并通过传递和使用创建的行为(例如对Apple的不同谓词)将方法的行为参数化。
// 定义Apple类 public class Apple{ private Integer weight = null; private String color = null; public Apple(Integer weight, String color){ this.weight = weight; this.color = color; } public Integer getWeight(){ return this.weight; } public String getColor(){ return this.color; } }
① 将筛选条件抽象化,作为参数进行传递
// 定义一个接口来对选择标准建模 public interface ApplePredicate{ boolean test (Apple apple); } // 用ApplePredicate的多个实现代表不同的选择标准 public class AppleHeavyWeightPredicate implements ApplePredicate{ public boolean test(Apple apple){ return apple.getWeight() > 150; } } public class AppleGreenColorPredicate implements ApplePredicate{ public boolean test(Apple apple){ return "green".equals(apple.getColor()); } } public class AppleRedAndHeavyPredicate implements ApplePredicate{ public boolean test(Apple apple){ return "red".equals(apple.getColor()) && apple.getWeight() > 150; } } // 根据抽象条件筛选 public static List<Apple> filterApples(List<Apple> inventory, ApplePredicate p){ List<Apple> result = new ArrayList<Apple>(); for(Apple apple: inventory){ if(p.test(apple)){ result.add(apple); } } return result; } // 传递代码 List<Apple> inventory = Arrays.asList(new Apple(80,"green"), new Apple(155, "green"), new Apple(120, "red")); List<Apple> heavyApples = filterApples(inventory, new AppleHeavyWeightPredicate()); List<Apple> greenApples = filterApples(inventory, new AppleGreenColorPredicate());
② 将输出信息抽象化,作为参数传递
// 定义一个接口来对输出信息建模 public interface AppleFormatter{ String accept (Apple apple); } public class AppleFancyFormatter implements AppleFormatter{ public String accept(Apple apple){ String characteristic = apple.getWeight() > 150 ? "heavy" : "light"; return "A " + characteristic + " " + apple.getColor() +" apple"; } } public class AppleSimpleFormatter implements AppleFormatter{ public String accept(Apple apple){ return "An apple of " + apple.getWeight() + "g"; } } public static void prettyPrintApple(List<Apple> inventory, AppleFormatter formatter){ for(Apple apple: inventory){ String output = formatter.accept(apple); System.out.println(output); } } public void main(String[] args){ __1__ my = new __1__(); List<Apple> inventory = new ArrayList<Apple>(); my.prettyPrintApple(inventory, new AppleFancyFormatter()); }
③ 使用匿名类
List<Apple> redApples = filterApples(inventory, new ApplePredicate() { public boolean test(Apple apple){ return "red".equals(apple.getColor()); } });
④ 使用Lambda表达式
List<Apple> result = filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));
⑤ 将List类型抽象化
public interface Predicate<T>{ boolean test(T t); } public static <T> List<T> filter(List<T> list, Predicate<T> p){ List<T> result = new ArrayList<T>(); for(T e: list){ if(p.test(e)){ result.add(e); } } return result; } List<Apple> redApples = filter(inventory, (Apple apple) -> "red".equals(apple.getColor())); List<Integer> evenNumbers = filter(numbers, (Integer i) -> i % 2 == 0);
⑥ 例子:用Comparator 来排序
// java.util.Comparator public interface Comparator<T> { public int compare(T o1, T o2); } // 给sort()函数传递一个匿名类 inventory.sort(new Comparator<Apple>() { public int compare(Apple a1, Apple a2){ return a1.getWeight().compareTo(a2.getWeight()); } }); // 给sort()函数传入一个Lambda表达式 inventory.sort( (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
⑦ 例子:用Runnable 执行代码块
// java.lang.Runnable public interface Runnable{ public void run(); } // 使用这个接口创建执行不同行为的线程 Thread t = new Thread(new Runnable() { public void run(){ System.out.println("Hello world"); } }); // 用Lambda表达式的话,看起来是这样 Thread t = new Thread(() -> System.out.println("Hello world"));
⑧ 例子:GUI 事件处理
Button button = new Button("Send"); button.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { label.setText("Sent a1c0 !!"); } }); // Lambda表达式 button.setOnAction((ActionEvent event) -> label.setText("Sent!!"));
相关文章推荐
- Java8 in action(1) 通过行为参数化传递代码--lambda代替策略模式
- Java8 in action(1) 通过行为参数化传递代码--lambda代替策略模式
- 《Java in Action》-1 第2章 通过行为参数化传递代码
- java8实战一:通过行为参数化传递代码
- java-8 行为参数化,代码传递-1
- 通过行为参数传递代码
- Cocos2d-x中通过JNI进行C++调用Java代码
- Cocos2d-x游戏开发之Cocos2dx通过JNI调用Android的Java代码(webView实例)
- Python3 -- 通过pandas实现快速创建java实体代码模板
- java通过sql代码生成Dao和实体类
- javat图片缩小代码(测试通过)
- 通过JNI在JAVA与C程序中传递参数
- Java Map 通过 key 或者 value 过滤的实例代码
- java8新特性(一):行为参数化
- C#(服务器)与Java(客户端)通过Socket传递对象(序列化 json)
- 通过SmartInvoke运用java与flex轻松构建cs程序(数据类型的传递)
- android之通过java代码实现PreferenceActivity
- Java8之行为参数化
- 通过反射实现Java下的委托机制代码详解
- 将Opencv java中的Mat通过jni传递到C++中的方法