Closed naceee closed 2 weeks ago
Solution one: it can be a class attribute of AbstractClass
?
Solution two (probably better): it can become a module-global variable instead of a class variable?
In both cases the code within the classes need to be adapted to account for the different variable name.
At a call with Niko and Olaf we came to the following conclusions:
m
should be an attribute of the factory function and fed to the class constructors.GetArchive
instead of MOArchive
.MOArchiveAbstract
.A possible aspect: the module should ideally not depend on Fraction
to be installed, even if it is (decided to be) the default type for the hypervolume.
Do you think it is a reasonable assumption, that the different classes will always be initialized using the GetArchive
function or not?
GetArchive
function, not in each class separately, probably some other checks could be put there as wellDo you think it is a reasonable assumption, that the different classes will always be initialized using the
GetArchive
function or not?
Probably not, because this would break backwards compatibility which is generally not desirable (at all).
It is also be bug prone to assume the user is not using a class directly and it creates an unnecessary dependency when the class is not self sustained. If such assumption is made, at the very least the class names must start with an underscore to make it clear that they are for internal use only.
Thanks, that makes sense and I implemented it this way - so that every class can be used on it's own as well. So I think I can close this issue now.
We have 3 classes, that are dealing with archiving points in 2, 3 and 4 dimensions: let's call them
class2
,class3
andclass4
. Each class needs to have implemented certain methods, lets saymethod1
andmethod2
, that's why we have an abstract base class calledAbstractClass
.Additionally
class3
andclass4
(but notclass2
) share some common methods - let's saymethod1
in this example, so we create a parent class for them calledParent34
, to avoid duplicating code.We also want to have one class variable
m
set to 42 for all of the classes by defualt, but we might want to change this. So our base classes look like this.To make the different classes easy to use for the user, we also have a factory function
ClassFactory
, where we return the correct class for the givenn
.Sometimes we would like to change the class variable
m
before creating a new instance of a class. Without using the factory method we could do something like this:But we would like the user to be able to do this without specifying the class
Class2
. How can we do this?m
an instance variable, but then we have to pass it everytime we create a new instance of the class....?