前一篇关于domain object的例子。(当然没写完整。)
如果不那么写,会是怎样的效果呢?我觉得我之后列出的代码可能是非常常见的一种写法。而且相比于ddd,大家可能更加熟悉。
@Setter
@Getter
public class Account {
private String email;
private String password;
}
public interface IAccountDao {
void insert(Account account);
void update(Account account);
Account findById(String email);
}
public class AccountService {
@Autowired
private IAccountDao accountDao;
public void changePassword(String email, @NonNull String oldPassword, @NonNull String newPassword){
Account account = accountDao.findById(email);
if(oldPassword.equals("")){
throw new IllegalArgumentException("password cannot be empty");
}
if(!account.getPassword().equals(encryptPassword(oldPassword))){
throw new IllegalArgumentException("old password is wrong");
}
account.setPassword(encryptPassword(newPassword));
accountDao.update(account);
}
public void createNewAccount(String email, @NonNull String password){
Account account = new Account();
account.setPassword(password);
account.setEmail(email);
accountDao.insert(account);
}
private String encryptPassword(String password){
// TODO non implmented
return password;
}
}