File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Can some pattern help here? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Can some pattern help here?" Watch "Can some pattern help here?" New topic

Can some pattern help here?

Prasma Kankut

Joined: Oct 15, 2003
Posts: 21
Assume the following case:

The price to transport a shipment depends on 2 things
i) the distance from origin to destination and
ii) the weight of the shipment

For example,
If the transportation distance is less than 100 kms, the cost is 10$
If the transportation distance is between 100 and 200 kms, the cost is 20$
If the transportation distance is greater than 200 kms, the cost is 30$

If the shipment weight is less than 10 kgs, the cost is 10$
If the shipment weight is between 10 and 20 kgs, the cost is 20$
If the shipment weight is greater than 20 kgs, the cost is 30$

So, to transport a 50 kg shipment for 500 kms, the total cost is 60$.

Now, the shipment will have a getTotalCost() method, which will calculate the total cost, based on the above rules. It can be done with simple if/else checks, but ofcourse that is the worst way of doing.

Is there any pattern, which can help me here?

Strategy looks close, but I'm not sure. Any clues.

Many thanks

<i>The fact that today we know more than yesterday is good news about today, not bad news about yesterday - Ilja Preuss</i>
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I like Strategy but I almost always use fixed values to get a strategy from a map ... your ranges of values are a bit tougher.

For something this simple I'd probably just work through a collection of distance / rate pairs.

If you really must be objecty or if things got a bit more complex you could use a chain of widgets with this method:

I don't know if that's quite Chain Of Responsibility or not.

If your relationships are so neatly mathematical and you don't want to be at all objecty you could even do

Any other thoughts?
[ January 05, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Another "objecty" solution would be to replace

if distance < pair.distance


if pair.appliesTo(distance)

But it's most likely overkill, and easy to refactor to should it become valuable.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Can some pattern help here?
It's not a secret anymore!