ciren/cilib

Remove getClone() from source

gpampara opened this issue · 4 comments

The getClone() is an effect of the bean way of writing java, resulting in zero benefit for a large amount of pain. It should be removed with prejudice.

How else do we get a polymorphic copy constructor?

How are they polymorphic? Are you talking about deep clones?

e.g:
AbstractAlgorithm alg1 = new PSO();
AbstractAlgorithm alg2 = alg1.getClone();

Without the getClone, we can't create a copy of PSO without explicitly using the PSO copy constructor. There's a lot of cases in cilib where we don't know what object type is stored in alg1 (e.g. where it is set externally), and thus won't be able to select the correct copy constructor. With getClone the object itself is in charge of knowing which copy constructor to use.

Unless java or fj or something else brought in a better mechanism to deal with this situation, or you have some plan to avoid these situations, we are stuck with getClone.

This is largely, in fact solely, due to the fact that the XML based simulator was restrictive and we needed to be able to do things "on the fly". That was the wrong way to do things because the logic that created the instances and the logic that used the instances were tightly coupled.

The new simulator will force the needed separation. Additionally, in your example, you needed two PSO instances. You would need to provide them ahead of time to ensure that the class that uses them can be instantiated in the first place.

public class MyClass {
    public MyClass(List<PSO> psosINeed) {...}
}