[英]Creating types as wrapper for primitive types
Quite often we have projects with a lot of entity types (Hibernate/JPA, ...). 通常,我们的项目具有很多实体类型(Hibernate / JPA等)。 The entity types have a primitive type like an int, long or string for an ID. 实体类型具有基本类型,例如ID的int,long或string。 We have DAOs and Services that take and return this primitives. 我们有采用和返回此原语的DAO和服务。
long doSomething(long blubId, long blabId, long fooId, long barId, ...)
As you can see, it can be very confusing. 如您所见,这可能非常令人困惑。 I'd like to take advantage from the static type checking and have types for it. 我想利用静态类型检查并为其提供类型。
long doSomething(BlubId blubId, BlabId blabId, FooId fooId, BarId barId, ...)
But this seems to be inefficient. 但这似乎效率很低。 Do you have any ideas how to solve this issue? 您是否有解决此问题的想法?
Note, handling with primitives is sometimes required in the JEE context (JSF backed beans, persistency, ...) 请注意,有时在JEE上下文中需要使用基元进行处理(JSF支持的bean,持久性等)。
This seems like a prime candidate for the builder pattern where you have a DoSomethingOptions
class with a DoSomethingOptionsBuilder
which creates the options explicitly via named methods to avoid confusion of parameter order. 这似乎是构建器模式的主要候选者,在该模式中,您具有一个DoSomethingOptions
类和一个DoSomethingOptionsBuilder
,该类通过命名方法显式创建选项,以避免混淆参数顺序。
To be honest, I don't see your initial approach being terribly inefficient, but that really depends on how often your doSomething
method is being called and how long the actual logic of the method takes. 老实说,我认为您的初始方法效率极低,但这实际上取决于doSomething
方法被调用的频率以及该方法的实际逻辑需要多长时间。 Obviously using wrappers to primitives will take more space and a marginal amount of time, but it's nearly inconsequential compared to any real computation. 显然,对原始对象使用包装器会占用更多空间和少量时间,但是与任何实际计算相比,这几乎是无关紧要的。
据我了解,您可能会对查找已经编写的Java包装类Java原始包装类感兴趣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.