2019独角兽企业重金招聘Python工程师标准>>>
一、用户组件的功能
使用java8 lambda表达式实现实现世界的一个例子:用户组件。此用户组件有以下几个操作:获取用户列表,获取单个用户,增加用户,删除用户,更新用户。所有的操作都是使用UserRequest对象并返回一个UserResponse对象。
在各个操作中,需要在方法执行前进行请求合法性验证,方法执行后进行特殊的响应处理,检查所有的响应特性
执行日志记录或其他操作。
在原来的处理方式中,需要在每个操作中都把要执行的请求方法包括在至少一个try-catch语句块中。在java8中,可以创建一个的方法统一处理方法执行前后的操作,如源代码中的callComponent方法,使代码更加简单,更少的代码重复。
二、代码样例
1.定义一个用户类User
2.定义用户组件的接口UserComponent,UserRequest,UserResponse
3.使用lambda表达式实现用户组件的接口UserComponentImpl
4.实现Adapter类UserComponentAdapter
5.创建测试类UserComponentTest
public class User {Integer id;String name;Integer age;User() {}User(int id, String name, int age) {this.id = id;this.name=name;this.age = age;}public String toString() {return "" + id + "-" + name + "-" + age;}
}
public interface UserComponent {UserResponse fetchAllUsers(UserRequest req);UserResponse fetchUser(UserRequest req);UserResponse deleteUser(UserRequest req);UserResponse updateUser(UserRequest req);UserResponse insertUser(UserRequest req);
}
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;public class UserComponentImpl implements UserComponent {List<User> users = new ArrayList<User>();public UserResponse fetchAllUsers(UserRequest req) {return new UserResponse(users);}public UserResponse fetchUser(UserRequest req) {return new UserResponse(users.stream().filter(u -> u.id.equals(req.user.id)).collect(Collectors.toList()));}public UserResponse deleteUser(UserRequest req) {users = users.stream().filter(u -> (!u.id.equals(req.user.id))).collect(Collectors.toList());return new UserResponse(true);}public UserResponse updateUser(UserRequest req) {List<User> list = users.stream().filter(u -> u.id.equals(req.user.id)).collect(Collectors.toList());if (list.size() > 0) {User user = list.get(0);user.name = req.user.name;user.age = req.user.age;return new UserResponse(true);}return new UserResponse(false);}public UserResponse insertUser(UserRequest req) {users.add(req.user);return new UserResponse(true);}
}
import java.io.IOException;
import java.util.List;
import java.util.function.Function;public class UserComponentAdapter {UserComponent uc = new UserComponentImpl();UserResponse callComponent(UserRequest request,Function<UserRequest, UserResponse> func) {try {// 验证请求的合法性UserResponse response = func.apply(request);// 特殊的响应处理,检查所有的响应特性// 执行日志记录和/或其他操作return response;} catch (Exception e) {e.printStackTrace();// 进行异常处理return new UserResponse(true);}}List<User> fetchAllUsers() throws IOException {return callComponent(new UserRequest(), uc::fetchAllUsers).users;}User fetchUser(Integer id) throws IOException {return callComponent(new UserRequest(new User(id, "", 0)),uc::fetchUser).users.get(0);}boolean deleteUser(Integer id) throws IOException {return callComponent(new UserRequest(new User(id, "", 0)),uc::deleteUser).success;}boolean updateUser(User user) throws IOException {return callComponent(new UserRequest(user), uc::updateUser).success;}boolean insertUser(User user) throws IOException {return callComponent(new UserRequest(user), uc::insertUser).success;}
}
import java.io.IOException;public class UserComponentTest {public static void main(String[] args) throws IOException {UserComponentAdapter uca = new UserComponentAdapter();uca.insertUser(new User(1, "张三", 12));uca.insertUser(new User(2, "李四", 21));uca.insertUser(new User(3, "王五", 32));uca.insertUser(new User(4, "赵六", 43));System.out.println(uca.fetchAllUsers());System.out.println(uca.fetchUser(2));System.out.println(uca.updateUser(new User(3, "王五五", 55)));System.out.println(uca.deleteUser(4));System.out.println(uca.fetchAllUsers());}
}
public class UserRequest {User user;public UserRequest() {}public UserRequest(User user) {this.user = user;}
}
import java.util.List;public class UserResponse {List<User> users;boolean success = true;public UserResponse(boolean success) {this.success=success;}public UserResponse(List<User> users) {this.users = users;}
}
三、参考资料
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html.
http://www.dreamsyssoft.com/java-8-lambda-tutorial/