Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Set implementation and call
- // i.e. Returns (creates) the concrete implementation of the object,
- // subsequently operation is called on the concrete implementation
- ab.Implementor = new ConcreteImplementorA();
- ab.Operation();
- // Set the context with a strategy
- // i.e. Sets the concrete strategy into the context, concrete implementation of the class not
- // directly available as a data object (only the algorithm is available).
- context = new Context (new ConcreteStrategyA());
- context.contextInterface();
- // Strategy can be reused instead of creating a new instance every time it is used.
- // Sort example
- MergeSort mergeSort = new MergeSort();
- QuickSort quickSort = new QuickSort();
- ...
- context = new Context (mergeSort);
- context.Sort();
- ...
- context = new Context (quickSort);
- context.Sort();
- ...
- context = new Context (mergeSort);
- context.Sort();
- class Sorter abstract
- {
- virtual void Sort() = 0;
- }
- // MergeSorter IS A Sorter
- class MergeSorter : public Sorter
- {
- virtual void Sort() override;
- }
- class SortStrategy abstract
- {
- virtual void Sort() = 0;
- }
- // Sorter HAS A SortStrategy
- class Sorter
- {
- Sorter(SortStragety *strategy) : mStrat(strategy) {}
- void Sort() {mStrat->Sort();}
- SortStrategy *mStrat;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement