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?
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.
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.
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.
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.
精彩评论