Java – get customer orders: set getallorders() and set getallorders()

I haven't done much Java programming, so many unresolved ORM problems come to mind and seem quite direct to more experienced people

Suppose we have two classes: customer and order The customer class implements a method called listallorders. What should be the signature of this method?

>Set < order > getallorders()// OOP mode > setting < integer > getallorders()// DB friendly mode, based on the assumption that each order is assigned a unique int ID > int [] getallorders()// In a database friendly way, optimize > set < OrderID > getallorders()// OOP mode, optimization > other? Combination of method overloading above?

My idea is to fire the third option immediately, because optimization is immature, unnecessary, and likely to lead to more trouble rather than good

Choosing between the first and second options, the main argument seems to be that in many cases returning a set of orders instead of ID's would be overkill

The first option always needs to pre load the order into memory. Although some order attributes can be processed by delayed loading, the order object itself still needs more memory allocation rather than a set of raw IDs. Another reason seems to be that delayed loading will not bring me the advantage of using the final modifier to force the invariance of the order field

However, option 2 does not really encapsulate the order number, that is, if you decide to start using the string UUID or long-term as the order identifier instead of an integer, you need to strictly rewrite it Obviously, this situation can be alleviated by introducing a new lightweight object called OrderID (the fourth method)

Well, at this point, I thank ORM and Java masters for their help!

Solution

It always helps to know what the caller wants If the order ID returned each time is not optimized, the caller will have to find each order On the other hand, if they don't need all this information most of the time, there will be no return

I go to option 1 most of the time But if it is limited by memory like a smartphone, I think I will choose lazy expansion

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>