I'm writing a program that recognizes
patterns in data and tells me something about that pattern. So I create a Pattern class and give it a recognize method, and an array field to hold what I find out (as well as other fields to contain the data itself). I'm doing a lot of patterns so performance is an issue.
public class Pattern{
private int[] characteristics = new int[5];
public void recognize(){
.....
}
}
Time to write my method. Well, recognizing the pattern is not simple. I need to execute a whole bunch of different tests. How to structure the code?
My instinct is to create a bunch of subroutines that recognize() executes one after the other to look for each category of pattern. But you can't create sub-methods in
Java.
So - create private methods within the body of the class and call those? But then all my variables go out of scope and I have to pass through all the relevant ones then pass an array of them back, then re-assign them to the original variables... seems very inelegant. Or, define all the variables at class level as fields, but again this seems inelegant since they're not object "properties" and should be disposed of when the method completes.
Or - write a monolithic method full of if{} blocks that check whether the pattern type has been found yet before executing the next block of code... again seems inelegant. My first attempt has done something like this, but I used labelled code blocks using { } and the break command, which I understand is frowned upon.
Or - make some nested classes called PatternRecognizer etc which contain all the relevant routines as methods and variables as fields? Then dispose of the PatternRecognizer object from the recognize() method when I'm finished? Not sure about this since I've never created a nested class before.
Any thoughts appreciated!