Java design issue

I work on the application as a hobby. It is written in Java and is part of a system for calculating decompression schemes for scuba diving. The problem is that I have a class that stores my dive plan, but this is not a problem because it works fine, but I had the feeling that it could be designed better, so I would like to ask for some feedback.

Now I have the following.

A DivePlan class that has an ArrayList DiveOpperations ( ArrayList<DiveOpperation> ). The DivePlan class has 3 functions. One was planning a descent, one was planning an ascent, and one was planning a flat dive at the same depth. All of these functions add a DiveOpperation object to an ArrayList. However, ascents and descents have some other attributes as dives, which remain at the same depth. For example, the speed in m / s of ascent / descent. I set the speed to 0 for flat dives, but it is not. I know that I have to create separate classes that extend DiveOpperation , but I don’t know if it is an ascent , descent or flatdive when I get it from an array.

What will be a good design for such functions?

+4
source share
4 answers

How to enable the selection of verticalSpeed ?

With a value of 0 DiveOperation represent a plane, and with a positive or negative value represents an ascent or descent, respectively?

If you have other properties besides ascent / descent, you probably want to turn DiveOperation into an abstract class and create three subclasses of AscentOperation , DescentOperation and FlatDiveOperation . If I understand you correctly, you have problems with, for example, iteration, since you do not know the actual type of DiveOperation . This can be solved using a lot of instanceof checks (ugly!) Or using a template (much better than imo). In this case, you could have something like a DecompressionCalculatorVisitor that every DiveOperation visits in DiveOperation .

Take a look at my answer here for a detailed example visitor template.

+1
source

A polymorphic solution is to extend the DiveOperation class into AscentOperation, DescentOperation, and FlatOperation and preserve the existing ArrayList. I'm not sure if you need to do this here, given how simple the implementation is.

You would be better off developing a good set of unit tests for your calculations, so if you need to add complexity to accommodate more functions, you can reorganize more easily.

+2
source

I'm not sure I understood your problem exactly, but 1. I don’t think you need to know what the real DiveOperation type is. Use the command template and just call the command method. The team will do its job. 2. If you still need to know the type you can get using the instanceof operator.

0
source

Expand it. You can determine which subclass it is (ascending, descending, flat) by taking it from the array and then checking (object instanceof AscentDive) , etc.

Remember that generics are erased at runtime. Thus, the ArrayList that you actually defined contains only "Objects" from the perspective of the JVM. A common value exists to check compilation time. If you subclass DiveOperation and define ArrayList<DiveOperation> , you can always add a subclass to this list.

0
source

All Articles