I always have a problem with creating a class in which the programmer would understand the correct call to the method and there would be no danger for the data to execute any method, and the variables are set by another method. Therefore, I usually use flags and If statements to be safe:
class foo {
boolean isInitialised = FALSE;
boolean isSthDone = FALSE;
float importantData;
public void initialise {
...
isInitialised = TRUE;
}
public void doSth1 {
if (isInitialised) {
importantData = 2134234;
} ...
isSthDone = TRUE;
}
public void doSth2 {
if (isInitialised && isSthDone1) {
...
}
}
}
This type of design does not give any clue how to use the algorithm - which method should be performed first, is there a design pattern for this problem?
source
share