在日常编码中,我们会遇到这样一个场景:把一个类型的对象转换成另一个对象,而这两者之前的转换强调的是"值(Value)"的等价转换,两者之间并没有继承与被继承的关系,也并不是像浮点数转整数这种语法意义上的转换关系。如下面举的这个例子:"用户"这个对象定义了User和UserDto两种Bean class来表示,二者所代表的"值"都是一致的,只是一个是业务逻辑层面的,一个是数据访问层面的。二者之前常常会发生转换,这个时候可以使用转换器模式:
类图:
代码:
/**
* User class
*/
public class User {
private String firstName;
private String lastName;
private boolean isActive;
private String userId;
/**
* @param firstName user's first name
* @param lastName user's last name
* @param isActive flag indicating whether the user is active
* @param userId user's identificator
*/
public User(String firstName, String lastName, boolean isActive, String userId) {
this.firstName = firstName;
this.lastName = lastName;
this.isActive = isActive;
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public boolean isActive() {
return isActive;
}
public String getUserId() {
return userId;
}
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return isActive == user.isActive && Objects.equals(firstName, user.firstName) && Objects
.equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
}
@Override public int hashCode() {
return Objects.hash(firstName, lastName, isActive, userId);
}
@Override public String toString() {
return "User{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
+ ", isActive=" + isActive + ", userId='" + userId + '\'' + '}';
}
}
/**
* User DTO class
*/
public class UserDto {
private String firstName;
private String lastName;
private boolean isActive;
private String email;
/**
* @param firstName user's first name
* @param lastName user's last name
* @param isActive flag indicating whether the user is active
* @param email user's email address
*/
public UserDto(String firstName, String lastName, boolean isActive, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.isActive = isActive;
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public boolean isActive() {
return isActive;
}
public String getEmail() {
return email;
}
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
UserDto userDto = (UserDto) o;
return isActive == userDto.isActive && Objects.equals(firstName, userDto.firstName) && Objects
.equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
}
@Override public int hashCode() {
return Objects.hash(firstName, lastName, isActive, email);
}
@Override public String toString() {
return "UserDto{" + "firstName='" + firstName + '\'' + ", lastName='" + lastName + '\''
+ ", isActive=" + isActive + ", email='" + email + '\'' + '}';
}
}
/**
* Generic converter, thanks to Java8 features not only provides a way of generic bidirectional
* conversion between coresponding types, but also a common way of converting a collection of objects
* of the same type, reducing boilerplate code to the absolute minimum.
* @param <T> DTO representation's type
* @param <U> Domain representation's type
*/
public class Converter<T, U> {
private final Function<T, U> fromDto;
private final Function<U, T> fromEntity;
/**
* @param fromDto Function that converts given dto entity into the domain entity.
* @param fromEntity Function that converts given domain entity into the dto entity.
*/
public Converter(final Function<T, U> fromDto, final Function<U, T> fromEntity) {
this.fromDto = fromDto;
this.fromEntity = fromEntity;
}
/**
* @param userDto DTO entity
* @return The domain representation - the result of the converting function application on dto entity.
*/
public final U convertFromDto(final T userDto) {
return fromDto.apply(userDto);
}
/**
* @param user domain entity
* @return The DTO representation - the result of the converting function application on domain entity.
*/
public final T convertFromEntity(final U user) {
return fromEntity.apply(user);
}
/**
* @param dtoUsers collection of DTO entities
* @return List of domain representation of provided entities retrieved by
* mapping each of them with the convertion function
*/
public final List<U> createFromDtos(final Collection<T> dtoUsers) {
return dtoUsers.stream().map(this::convertFromDto).collect(Collectors.toList());
}
/**
* @param users collection of domain entities
* @return List of domain representation of provided entities retrieved by
* mapping each of them with the convertion function
*/
public final List<T> createFromEntities(final Collection<U> users) {
return users.stream().map(this::convertFromEntity).collect(Collectors.toList());
}
}
/**
* Example implementation of the simple User converter.
*/
public class UserConverter extends Converter<UserDto, User> {
/**
* Constructor.
*/
public UserConverter() {
super(userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
userDto.getEmail()),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
user.getUserId()));
}
}
/**
* The Converter pattern is a behavioral design pattern which allows a common way of bidirectional
* conversion between corresponding types (e.g. DTO and domain representations of the logically
* isomorphic types). Moreover, the pattern introduces a common way of converting a collection of
* objects between types.
*/
public class App {
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
Converter<UserDto, User> userConverter = new Converter<>(
userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
userDto.getEmail()),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId()));
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
User user = userConverter.convertFromDto(dtoUser);
System.out.println("Entity converted from DTO:" + user);
ArrayList<User> users = Lists.newArrayList(new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
System.out.println("Domain entities:");
users.forEach(System.out::println);
System.out.println("DTO entities converted from domain:");
List<UserDto> dtoEntities = userConverter.createFromEntities(users);
dtoEntities.forEach(System.out::println);
}
}