Design pattern > Comportementaux > Strategy
Dans ce patron, une famille d'algorithmes est encapsulée de manière à ce qu'ils soient interchangeables.
Les algorithmes peuvent changer indépendamment de l'application qui s'en sert.
Il comporte trois rôles : le contexte, la stratégie et les implémentations.
La stratégie est l'interface commune aux différentes implémentations - typiquement une classe abstraite.
Le contexte est l'objet qui va associer un algorithme avec un processus.
Strategy (SortStrategy)
- declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
ConcreteStrategy (QuickSort, ShellSort, MergeSort)
- implements the algorithm using the Strategy interface
Context (SortedList)
- is configured with a ConcreteStrategy object
- maintains a reference to a Strategy object
- may define an interface that lets Strategy access its data.
QuickSorted list
Anna
Jimmy
Samual
Sandra
Vivek
ShellSorted list
Anna
Jimmy
Samual
Sandra
Vivek
MergeSorted list
Anna
Jimmy
Samual
Sandra
Vivek