How to ensure that builder pattern is completed?

后端 未结 7 1660
鱼传尺愫
鱼传尺愫 2021-02-05 13:38

EDIT: I am not worried about being called in the wrong order since this is enforced through using multiple interfaces, I am just worried about the terminal method getting called

7条回答
  •  攒了一身酷
    2021-02-05 14:14

    You could write a rule for PMD or Findbugs if you really want to enforce it in the code. This would have the advantage that it is already available at compile time.


    Runtime: If you only want to make sure the users call your builder in the correct order then use separate interfaces for each step.

    grantUser() will return ISetPermission which has the method permissionTo(), which will return an IResourceSetter which has the method item()...

    You can add all those interfaces to one builder, just make sure that the methods return the correct interface for the next step.

提交回复
热议问题