40 likes | 165 Vues
This guide explores effective ways to implement various algorithms to solve a given problem. The focus is on using the Strategy Pattern to organize the implementation process, allowing for easy selection and execution of different algorithms (Alg1, Alg2, ..., AlgN) based on provided input. For instance, sorting can utilize multiple algorithms such as Insertion Sort, Quick Sort, or others seamlessly through a common interface. This structured approach not only improves code organization but also enhances flexibility in choosing the appropriate algorithm for specific scenarios.
E N D
About how to implement algorithms Fall 2013, HaidongXue
One Problem, Multiple Algorithms • Given a problem • Problem Input: IN • Algorithms: • Alg1(IN), Alg2(IN), … , AlgN(IN) • E.g.: • your term project • sorting
A good way to implement them: the Strategy Pattern AbstractAlgorithm solve() ConcreteAlgorithm2 solve() ConcreteAlgorithmN solve() ConcreteAlgorithm1 solve() …
Example: sorting uses Tester AbstractSortingAlgorithm sort() InsertionSort sort() XSort sort() Quicksort() sort() … Let’s do it!