项目中的“坏”味道
高内聚、低耦合
系统能做什么事情,一目了然。
public class Account {
private Long id;
private Long accountNumber;
private BigDecimal available;
public void withdraw() {
// 转入操作
available = abailable + money;
}
public void deposit(BigDecimal money) {
//转出操作
if (available < monrey ) {
throws new InsufficientMoneyException();
}
abailable = available - money;
}
}
充血模型(引起实体变化的因素放入实体中) ——> 贫血模型POJO(Martin Fowler 提出)MicroService ——> 贫血失忆症
public interface AccountRepository {
// .....
}
public class AccountRepositoryImpl implments AccountRespository {
@Autowired
private AccountDao accountDAO;
@Autowired
private AccountBuilder accountBuilder; // 仓库工厂
@Override
public Account find() {
AccountDO accountDO = accountDAO.selectById(id);
return accountBuilder.toAccount(accountDO);
}
@Override
public Account find(Long accountNumber) {
AccountDO accountDO = accountDAO.selectByAccountNumber(AccountNumber);
return accountBuilder.toAccount(accountDO);
}
@Override
public Account save(Account account) {
AccountDO accountDO = accountBuilder.fromAccount(account);
if (accountDO.getId() != null) {
accountDAO.insert(accountDO);
} else {
accountDAO,update(accountDO);
}
return accountBuilder.toAccount(accountDO);
}
}
public interface BusiSafeService{
// ....
}
public class BusiSafeServiceImp implements