英文:
What are the alternatives of a data class in java?
问题
我正在重构汽车租赁系统的代码。租赁类记录了每次租赁的持续时间、里程和汽车类型。它有一个方法根据这些属性计算最终费用。
有3种(可能更多)不同日租价格的汽车类型。在ComputeCost方法中逐个检查每种汽车类型显然不是最佳解决方案。然而,多态似乎也不是合适的解决方案,因为不同的汽车类型没有实现额外的功能;它们只是有不同的费率。将ComputeCost方法移动到这些子类中的任何一个也不太合适,因为该方法更多地依赖于租赁属性,如里程和持续时间,而不仅仅是汽车类型。
英文:
I am refactoring a car rental system code. The rental class keeps record of each rental duration, mileage, and car type. It has a method to compute final cost based on these attributes.
There are 3 (possibly more) car types with different daily prices. Checking each car type in the ComputeCost method is obviously not the best solution. However, polymorphism doesn't seem the appropriate solution either, since different car types don't implement extra functionality; they just have different rates. Moving the ComputeCost method to each of these subclasses doesn't also seem appropriate, since the method depends more on rental attributes like mileage and duration than on car type.
答案1
得分: 1
听起来你真的想要一个Rate
类。
而且那个Rate
类有一个computeCost(Rental)
方法,用于计算使用给定费率的特定租赁的费用。
现在,如果不同费率之间唯一的区别是数值,那么Rate
类本身可以是一个相当基本的类,只需有几个字段。
英文:
It sounds like you really want a Rate
class.
And that Rate
class has a computeCost(Rental)
method that computes the cost of a given rental with a given rate.
Now if all that's different between the different Rates are numerical values, then the Rate
class can itself be a pretty basic class with a couple of fields.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论