简体   繁体   English

创建类型作为原始类型的包装器

[英]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.

相关问题 Java基本类型包装 - Wrapper around Java primitive types 基本数据类型的性能与其包装器类的关系 - Performance of Primitive Data types VS their Wrapper class 为非基本类型(类)定义包装器 - Define a Wrapper for non-primitive types(classes) Java中的原始包装器类是否被视为引用类型? - Are Primitive Wrapper Classes in Java Treated as Reference Types? 原始包装类型与Spring MVC的绑定性质 - Binding Properties of Primitive Wrapper Types with Spring MVC 在Java中,运算符是否对原始类型和原始包装器类执行相同的操作? - In Java, do operators perform identically on primitive types and primitive wrapper classes? 如何将String转换为基本类型或标准java Wrapper类型 - How to convert from String to a primitive type or standard java Wrapper types Java JLS是否指定了原始包装类型的提升? - Does the Java JLS specify promotion of primitive wrapper types? Java性能难题:包装器类比原始类型更快? - Java performance puzzler: wrapper classes faster than primitive types? JNI:将包装器类型用于原始数据类型以模拟C ++中的指针传递 - JNI:Using Wrapper Types for primitive data types to simulate passing by pointers in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM