-
Notifications
You must be signed in to change notification settings - Fork 0
LinearFitFactory and LinearFitStrategy
The most confusing part of this project is the implementation of linear fit algorithms. Linear fit algorithms consist of two main parts in LineFit: a Factory and a Strategy. These are both named after the design pattern they use (according to the gang of four book) and can be researched in detail if so desired. The short explanation is that a factory class is used to create an object at run time in a generic way. The Strategy class represent a specific way of doing something - in this case plotting a line. What makes this tricky in the case of LineFit is how these two classes interact with each other.
First, there are abstract classes for both of these classes, LinearFitFactory and LinearFitStrategy. LinearFitFactory is almost just an interface, but LinearFitStrategy implements a large amount of functionality of the strategies so that it does not have to be implemented each time in the child classes. This allows the child classes of LinearFitStrategy to only have to implement an equation to fit a line to the data and store it in the class variables.
The basic idea is that the Strategy class is a private class inside the factory class. This is so that the strategy, which actually calculates the fit, can only be created through the factory ensuring that it is done in a generic way. In order to create a new instance of a strategy, which is linked to a DataSet, you must go through its respective factory class. This also means that when a new algorithm is implemented, only one new file is created - the fit algorithm's factory class. This allows us to dynamically swap and switch between fit algorithms providing a large amount of flexibility. Unfortunately, this means that it is more complicated and confusing because of this combination of the two design patterns.
Because all of this functionality is in a separate package, it is very much abstracted away from LineFit. In fact, LineFit only knows about the abstract classes and the static list of all the implemented LinearFitFactories. Because of this, it only interacts in a generic way, calling the factories generate method to create the Strategies use to fit DataSets. This setup allows for a large amount of flexibility and allows LineFit to not "know" about all the different fit algorithms guaranteeing it is "plug and play" so to speak, with any new algorithms.
Because all of the basic functionality and structure can be confusing, inside the LineFit source is a template for creating new fit algorithms called AlgorithmFactoryTemplate inside the Template package. In addition, steps for creating the new algorithm can be found on its respective page, https://github.com/darksideprogramming/LineFit/wiki/Adding-an-Algorithm-to-LineFit.