I have to implement three different sorting algorithms using an Object-Oriented Approach and I have been thinking of the best method to combat this.
Basically, I think it should look like this:
-> Sort (Class, interface, Polymorphic)
Inherit:
-> Bubble Sort
-> Insertion Sort
-> QuickSort
If each of the sorts have the same interface then it would be easier to access the different methods of sorting, thus, meaning that when I come to add other sorting algorithms, I can easily implement them into the current design and class structure.
My questions are:
Is this a good approach to use?
Is there scope for me to use Templates? I.e. if I wanted to use Bubble, it would call the bubble sort, if I wanted to use Insertion it would call Insertion?
Sort
should be an interface or an abstract class, whereas bubble / insertion / quick-sort should be implementations / concrete classes.
It's also worth learning about the following:
Strategy Pattern:
http://en.wikipedia.org/wiki/Strategy_pattern
and
State Pattern:
http://en.wikipedia.org/wiki/State_pattern
As for templates, I don't think it's worth it in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With