I have a generic Vector<T> class and a generic Matrix<T> class and I was wondering if it would be a good idea to have both classes implement an interface.
Basically, I’m implementing two algorithms: AlgorithmA and AlgorithmB, both of which perform very similar operations (reset, average…etc) but with different algorithms and act on different structures:
AlgorithmA uses Vector<double> while AlgorithmB uses Matrix<Complex>.
The design I have so far:
abstract class AlgorithmArray
{
// Operator overloading
}
class AlgorithmAArray : AlgorithmArray
{
private Vector<double> _vector;
// Overrides
}
class AlgorithmBArray : AlgorithmArray
{
private Matrix<Complex> _matrix;
// Overrides
}
I would prefer to have AlgorithmAArray derive from Vector<T> and also implement an interface ‘IAlgorithmArray‘ (instead of the abstract class). Anyway, these algorithms are then used to simulate transmission/receiving between two locations:
public class CommunicationParameters
{
private AlgorithmArray _transmission;
private AlgorithmArray _receiving;
public void Compute()
{
if(_transmission != null)
_transmission.Compute();
if(_receiving != null)
_receiving.Compute()
}
}
Are there better ways to approach my problem ?
Note: The base class AlgorithmArray duplicates many of the operator/cloning…etc methods and I feel this could be avoided, perhaps using generics ?
Thanks !
I would suggest making two Algorithm classes that can take any data structure as a parameter and do their thing. I don’t see a need for all this OOP inheritance, it just adds complexity.