Benefits and drawbacks of method chaining and a possibility to replace all void return parameters by the object itself

前端 未结 9 1838
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 06:58

I am mostly interested in Java, but I think it\'s a general question. Recently I\'ve been working with Arquillian framework (ShrinkWrap) that uses a lot of meth

9条回答
  •  孤街浪徒
    2020-11-29 07:30

    This pattern is useful when there is series of update needs to be done on the same object and the update operations don't need to return any status of updation. For example I used this pattern in some api I wrote for database layer. For fetching some rows based on many criteria many conditions needs to be added to where clause. Using this pattern the criteria can be added as follows.

    CriteriaCollection().instance()
        .addSelect(Criteria.equalTo(XyzCriteria.COLUMN_1, value1))
        .addSelect(Criteria.equalTo(XyzCriteria.COLUMN_2, value2))
        .addSelect(Criteria.isIn(XyzCriteria.COLUMN_3, values3))
        .addOrder(OrderCriteria.desc(XyzCriteria.Order.COLUMN_1));
    

    Ultimately it improves the readability of the code.

提交回复
热议问题