Reputation: 1
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.
Upvotes: 0
Views: 123
Reputation: 308061
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.
Upvotes: 1