把服务对象(service)和资源库对象(repository)设计成接口是最常见的。但是这对接口化的认识还远远不够,我们需要更深入地去分析接口化设计和更全面地应用接口化编程。所以我们要讨论的是全面接口化,尤其是对领域模型接口化的认识。
- 领域接口化 -
通常的情况下我们会把领域模型设计成类(class),但是你有没有想过把领域模型设计成接口(interface)?比如:
public interface User {
// ...
}
public class UserImpl implements User {
// ...
}
这样的设计似乎没有任何价值,那么继续深入地看看。比如:
public class JpaUserRepository implements UserRepository {
// ...
@Override
public Optional<User> findById(String id) {
UserPO userPO = this.entityManager.find(UserPO.class, id);
return Optional.ofNullable(userPO).map(UserPO::toUser);
}
@Override
public User save(User user) {
UserPO userPO = this.entityManager.find(UserPO.class, user.getId());
userPO.setNickname(user.getNickname());
// ...
return this.entityManager.merge(userPO).toUser();
}
}
// ...
@Override
public User create(String id) {
return new JpaUser(id);
}
@Override
public Optional<User> findById(String id) {
JpaUser user = this.entityManager.find(JpaUser.class, id);
return Optional.ofNullable(user);
}
@Override
public User save(User user) {
JpaUser target = JpaUser.of(user);
return this.entityManager.merge(target);
}
// ...
}
public class JpaUser extends UserSupport {
// ...
public static JpaUser of(User user) {
if (user instanceof JpaUser) {
return (JpaUser) user;
}
var target = new JpaUser();
BeanUtils.copyProperties(user, target);
// ...
return target;
}
}
public interface ElasticsearchUserRepository
extends ElasticsearchRepository<ElasticsearchUser, String> {
// extends ElasticsearchRepository<User, String> // Not supported
}
public class DelegatingElasticsearchUserRepository implements UserRepository {
private final ElasticsearchUserRepository elasticsearchUserRepository;
public DelegatingElasticsearchUserRepository(ElasticsearchUserRepository elasticsearchUserRepository) {
this.elasticsearchUserRepository = elasticsearchUserRepository;
}
@Override
public User create(String id) {
return new ElasticsearchUser(id);
}
@Override
public Optional<User> findById(String id) {
return CastUtils.cast(this.elasticsearchUserRepository.findById(id));
}
@Override
public User save(User user) {
return this.elasticsearchUserRepository.save(ElasticsearchUser.of(user));
}
// ...
}