Java 领域模型之失血、贫血、充血、胀血模型

1.失血模型

失血模型仅仅包含数据的定义和getter/setter方法,业务逻辑和应用逻辑都放到服务层中。这种类在Java中叫POJO。

action
service: 核心业务(复杂度:重)
model:简单Set Get
dao :数据持久化

@Setter
@Getter
public class Commodity {

    private Long commodityId;

    private String commodityName;

}


@Setter
@Getter
public class Order {

    private Long orderId;

    private Long commodityId;

    private Date createTime;

}


2.贫血模型

贫血模型中包含了一些业务逻辑,但不包含依赖持久层的业务逻辑。这部分依赖于持久层的业务逻辑将会放到服务层中。

action
service :简单服务组合、事务管理(复杂度:中)
model:Set Get的基础上添加简单逻辑处理(复杂度:中)
dao:数据持久化

@Setter
@Getter
public class Commodity {

    private Long commodityId;

    private String commodityName;

}


@Setter
@Getter
public class Order {

    private Long orderId;

    private Long commodityId;

    private Commodity commodity;

    private Date createTime;

    public Order(Commodity commodity) {
        this.commodity = commodity;
    }

}

public class OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private CommodityMapper commodityMapper;


    public Order getOrderById(Long orderId) {
        Order order = orderMapper.getById(orderId);
        order.setCommodity(commodityMapper.getById(order.getCommodityId()));
        return order;
    }

}

3.充血模型

充血模型中包含了所有的业务逻辑,包括依赖于持久层的业务逻辑。使用充血模型的领域层是依赖于持久层。

action
service :简单服务、事务管理(复杂度:中-
model:Set Get的基础上添加复杂逻辑处理(复杂度:中+

@Setter
@Getter
public class Commodity {

    private Long commodityId;

    private String commodityName;

}


@Setter
@Getter
public class Order {

    private Long orderId;

    private Long commodityId;

    private Commodity commodity;

    private Date createTime;

    @Resource
    private CommodityMapper commodityMapper;

    public Commodity getCommodity() {
        return commodityMapper.getById(this.commodityId);
    }
    
}

public class OrderService {

    @Resource
    private OrderMapper orderMapper;

    public Order getOrderById(Long orderId) {
        return orderMapper.getById(orderId);
    }
}

4.胀血模型

胀血模型就是把和业务逻辑不想关的其他应用逻辑(如授权、事务等)都放到领域模型中。

action
model:Set Get的基础上添加复杂逻辑处理、事务处理(复杂度:重+

你可能感兴趣的:(Java,java,开发语言)