当为大且丰富的领域创建领域模型时,聚合、实体、值对象会变的复杂,如果创建一个对象的时候需要了解非常多的知识,领域的表述性将变得模糊
工厂的作用
- 从构造中分离出应用
- 隐藏了创建对象的复杂性,让使用方无须关注过多的细节。
- 增强了明确性,工厂方法的名字让人很容易知道创建对象意义和用途
- 将对象实例化集中在一个地方,在生成对象逻辑需要变更时很容易修改
2.封装内部逻辑

第一步改进


第二部改进

3.隐藏创建类型的决策
很多时候工厂创建的不是固定的对象,而是一个抽象类或者接口,里面要根据不同的策略去实例化。
4.工厂方法可以出现在聚合上

网友评论