4

I working on an application as an hobby project. It is written in Java and part of a system to calculate decompression scheme for scuba diving. The problem I have is in the class that stores my diveplan, well it is not realy a problem since it works fine but I got the feeling it can be designed better so i would like to ask for some feedback.

What I got now is the following.

A class DivePlan which has an ArrayList of DiveOpperations (ArrayList<DiveOpperation>). The DivePlan class has 3 functions. One to plan a descent, one to plan an ascent and one to plan a flat dive on the same depth. All these functions add a DiveOpperation object to the ArrayList. However ascents and descents have some other attributes as a dive that stays on the same depth. For example the speed in m/s of the ascent/descent. I set it to a speed of 0 for flat dives for now but that doesn't feel right. I'm aware I should make separate classes that extends the DiveOpperation but that way I don't know if it is an ascent, descent or flatdive class when I get it out of the array.

What would be a good design for such functionality?

2
  • 1
    This is impossible to answer. It sounds like your general approach is pretty good, and there's nothing inherently wrong with making a flat portion be an a/descent with a speed of 0 Commented Nov 23, 2010 at 19:37
  • Be aware that if you allow someone else to use your code, and as a result of a bug you provide an incorrect dive plan that leads to injury, you could be legally liable. Commented Nov 23, 2010 at 19:42

4 Answers 4

2

The polymorphic solution to this is to extend the DiveOperation class into AscentOperation, DescentOperation, and FlatOperation, and keep your existing ArrayList typing. I'm not sure you need to do this here given how simple the implementation seems.

You'd be better served developing a good set of unit tests for your calculations, that way if the need ever arises to add complexity to accomodate more functionality you can refactor more easily.

Sign up to request clarification or add additional context in comments.

5 Comments

is there a difference between a DescentOperation and a negative AscentOperation?
only difference that the speed is negative in an ascent and for validation because an ascent has a speedlimit for scuba divers while descent has not.
Hell if I know, I don't dive! His question is tagged with "inheritance" so I assumed he was seeking an OOP answer to specifying operation types rather than determining them logically.
Yes different objects sound like the best solutions to me. Only problem I had to determine which object I get out of the array. The visitor pattern of aioobe seems like a good solution to me.
Yes. The visitor pattern solves precisely the problem of determining the type, without relying on a mess of instanceofs.
1

How about letting a verticalSpeed variable decide?

With a value of 0 the DiveOperation represents a flatdive, and with a positive or negative value it represents a ascent or descent respectively?

If you have other properties than just ascent / descent speed, you probably want to turn the DiveOperation into an abstract class, and create three subclases AscentOperation, DescentOperation and FlatDiveOperation. If I understand you correctly however, you have troubles with for instance iteration, since you don't know the actual type of a DiveOperation. This can be solved either using lots of instanceof checks (ugly!) or using the visitor pattern (much better imo). You could in this case perhaps have something like a DecompressionCalculatorVisitor that visits each DiveOperation in turn.

Have a look at my answer over here for a detailed example of the visitor pattern.

2 Comments

yeah that is what i got now, but i wondering if there is a better design. Ascent and descent also have a different start and end depth which is the same for a flatdive. And I use different functions to calculate the decompression that is needed for the different types.
The visitor pattern looks nice. Will look into that.
0

I am not sure that I understood exactly you problem but 1. I do not think that you have to know what is the real type of DiveOperation. Use command pattern and just invoke the command method. The command will do its work. 2. If you still have to know the type you can retrieve it using instanceof operator.

Comments

0

Extend it. You can determined what sub-class it is (ascent, descent, flat) by taking it from the array and then checking (object instanceof AscentDive), etc..

Remember, generics are erased at runtime. So the ArrayList you have defined in reality only contains "Objects" from the point of view of the JVM. The generic is there for compile time validation. If you sub-class DiveOperation and define ArrayList<DiveOperation>, you can always add a sub-class to that list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.