如何将方法标记为强制方法?
假设您使用构建器模式创建一个名为 Person 的类,并假设 Builder 类包含方法 body()
、head()
、arms()
当然还有 build()
并且您认为方法 head()
和 build()
对于此类的用户来说是必需的。
如果可能的话,我们希望使用注释以某种方式将这些方法标记为强制方法。如果此类的用户尝试构建 Person 实例但忘记调用这些方法中的任何一个,我们希望收到某种警告 - 要么来自 java 编译器,要么来自 Eclipse 或 Maven,我们用它们来构建我们的实例。项目——任何一个都可以。
可以做吗?你会建议哪种方式?
Suppose you create a class names Person using the builder pattern, and suppose the Builder class contains methods body()
, head()
, arms()
and of course build()
and you consider methods head()
and build()
obligatory for the user of this class.
We would like to somehow mark these methods obligatory, if possible using annotations. If a user of this class tries to build a Person instance but forgot to call either of these methods, we would like to get some kind of warning - either from the java compiler, or maybe from Eclipse or Maven, which we use to build our projects - any of them would do.
Is it possible to do? Which way would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面是一个使用不同类型使某些部分成为强制性的示例(它也使您调用方法的顺序成为强制性的):
Edit
OP 对这个答案印象深刻,以至于他在 博客。这是对构建器模式的巧妙采用,值得在这里引用完整的处理方法。
Here is an example with using different types to make some parts mandatory (it also makes the order you call the methods mandatory):
Edit
The OP was so impressed with this answer that he wrote it up fully in a blog. It's such a clever take on the builder pattern that a full treatment deserves to be referenced here.
我相信正确使用构建器模式可以解决您遇到的问题。
我将创建类
PersonBuilder
,其中包含方法setBody()
和setArms()
以及所有其他可选参数设置方法。构建器的构造函数将采用所需的参数。然后方法build()
将返回Person
的新实例。或者,您可以将
Head
参数传递给方法build()
,但我更喜欢将其传递到构造函数中。I believe the correct use of the builder pattern would solve the issue you're having.
I would create class
PersonBuilder
which would contain the methodssetBody()
andsetArms()
and every other optional parameter setter method. The constructor of the builder would take the required parameters. Then the methodbuild()
would return the new instance ofPerson
.Alternatively you could pass the
Head
parameter to the methodbuild()
but I prefer passing it in the constructor instead.编译器没办法。
您可以做的是从构建器未正确初始化的
build()
方法中抛出运行时异常(并在 Maven 测试阶段调用一个测试),但您也可以使用
build(..)
接受一个HeadDetails
对象。这样,如果不指定强制参数,就无法调用构建。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 aHeadDetails
object. That way tou can't invoke build without specifying the obligatory parameters.如果确实是强制性的并且在 build() 方法中返回 Person,为什么不在 build() 方法中调用 body()、head()、arms() 呢?
[编辑]
简短示例:
Why not calling body(), head(), arms() in the build()-Method if it is really mandatory and returning Person in the build() method?
[edit]
Short example:
也许在
build()
内部,您可以检查是否已调用所有必需的方法。也许Person
实例有一些由build()
触发的内部健全性检查。当然,这会检查运行时行为,并且不是您所描述的静态分析。
Maybe inside of
build()
you could check if all the required methods have been called. Behaps thePerson
instance has some internal sanity check which is triggered bybuild()
.Of course this checks runtime behaviour and is no static analysis as you describe it.
无法在 Person 的构造函数中调用这些方法吗?
isn't possible to call these methods in Person's constructor ?