How to mark a method obligatory?

后端 未结 6 568
囚心锁ツ
囚心锁ツ 2020-12-30 10:21

Suppose you create a class names Person using the builder pattern, and suppose the Builder class contains methods body(), head(), arms()

6条回答
  •  长情又很酷
    2020-12-30 11:19

    No way with the compiler.

    You can do is throw a runtime exception from the build() method that the builder is not properly initialized (and have a test that is invoked in the maven test phase)

    But you can also have build(..) accept a HeadDetails object. That way tou can't invoke build without specifying the obligatory parameters.

提交回复
热议问题