失血模型
失血模型简单来说就是模型中只有属性的setter和getter方法,并且只是简单的赋值或直接返回属性值,是对一个实体类最简单的封装,其他所有的业务行为和数据存储由专门的服务类和DAO来完成。以一个Person类为例,包含姓名、生日、年龄的简单属性,只有判断今天是否生日和过生日(过生日后年龄会+1)两个行为。
public class Person extends Entity {/*** 姓名*/private String name;/*** 生日*/private Date birthday;/*** 年龄*/private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}public interface PersonService {/*** 判断今天是否生日*/boolean isTodayBirthday(Long personId);/*** 生日快乐,长一岁,age+1*/void happyBirthday(Long personId);}
贫血模型
- 贫血模型是在失血模型的基础上聚合了对应领域范畴的业务领域行为,不仅仅是简单的setter/getter,但在行为过程中对领域对象的状态发生的变化只停留在内存层面,不关心其数据的持久化,即不依赖Repository/DAO,把数据持久化放在service中按需处理。
public class Person extends Entity {/*** 姓名*/private String name;/*** 生日*/private Date birthday;/*** 年龄*/private Integer age;/*** 判断今天是否生日*/public boolean isTodayBirthday() {SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");return sdf.format(birthday).equals(sdf.format(new Date()));}/*** 生日快乐,长一岁,age+1*/public void happyBirthday() {age++;}public String getName() {return name;}public void setName(String name) {if (StringUtils.isBlank(name)) {// throw a exception}this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {if (birthday == null) {// throw a exception}this.birthday = birthday;}public Integer getAge() {return age;}public void setAge(Integer age) {if (age == null || age < 0) {// throw a exception}this.age = age;}}
充血模型
- 充血模型是在贫血模型的基础上,依赖repository,在业务领域行为执行过程中也负责数据的持久化存储。
public class Person extends Entity {@Autowiredprivate PersonRepository personRepository;/*** 姓名*/private String name;/*** 生日*/private Date birthday;/*** 年龄*/private Integer age;/*** 判断今天是否生日*/public boolean isTodayBirthday() {SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");return sdf.format(birthday).equals(sdf.format(new Date()));}/*** 生日快乐,长一岁,age+1*/public void happyBirthday() {age++;// 数据持久化personRepository.update(this);}public String getName() {return name;}public void setName(String name) {if (StringUtils.isBlank(name)) {// throw a exception}this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {if (birthday == null) {// throw a exception}this.birthday = birthday;}public Integer getAge() {return age;}public void setAge(Integer age) {if (age == null || age < 0) {// throw a exception}this.age = age;}}
胀血模型
- 这种模式下连service都不需要了,所有的业务逻辑、数据存储都放到一个类中。
public class Person extends Entity {@Autowiredprivate PersonMapper personMapper;/*** 姓名*/private String name;/*** 生日*/private Date birthday;/*** 年龄*/private Integer age;/*** 判断今天是否生日*/public boolean isTodayBirthday() {SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");return sdf.format(birthday).equals(sdf.format(new Date()));}/*** 生日快乐,长一岁,age+1*/public void happyBirthday() {age++;modify();}/*** 查询*/public static Person getById(Long id) {PersonDO personDO = personMapper.selectById(id);Person person = PersonConvertor.toPerson(personDO);return person;}/*** 新增*/public static Person create(String name, Date birthday, Integer age) {// 这里省略了事务管理,入参校验等,相当于service层需要做的都在这里处理Person person = new Person();person.setName(name);person.setBirthday(birthday);person.setAge(age);personMapper.insert(person);return person;}/*** 修改*/public void modify() {// 这里省略了事务管理,入参校验等,相当于service层需要做的都在这里处理personMapper.update(this);}public String getName() {return name;}public void setName(String name) {if (StringUtils.isBlank(name)) {// throw a exception}this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {if (birthday == null) {// throw a exception}this.birthday = birthday;}public Integer getAge() {return age;}public void setAge(Integer age) {if (age == null || age < 0) {// throw a exception}this.age = age;}}
总体描述
- 太轻 -> 数据驱动,而不够面向对象。
- 太重 -> 不稳定,难以被高度复用。
- 选用贫血模式还是充血模式,按照以下方案探讨:
- 缩小依赖范围
- 每一个微服务的内部的编码层面,倡导明确划分界限上下文,尽量以最小单元各司其职。减少不必要的耦合;从依赖范围来看,贫血模型的依赖范围是更小的。
- 关注点分离
- 关注点分离是对处理软件复杂性的指导原则,而单一职责是细致到指导如何面向对象设计一个类时的原则,他们都有一个共同的思想,就是解耦和增强内聚性,也就是我们经常说的“高内聚,低耦合”。
- 可复用性
- 领域之间尽量避免存在重复的业务逻辑这些方面就充分体现了可复用性这一点。
- 向稳定的方向依赖
- 试问如果一个软件系统,最核心的业务也扛不住变化的话,这个系统还有什么意义?
- 缩小依赖范围
