SHARE
TWEET

Untitled

a guest Jul 17th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Set implementation and call
  2. // i.e. Returns (creates) the concrete implementation of the object,
  3. // subsequently operation is called on the concrete implementation
  4. ab.Implementor = new ConcreteImplementorA();
  5. ab.Operation();
  6.      
  7. // Set the context with a strategy
  8. // i.e. Sets the concrete strategy into the context, concrete implementation of the class not
  9. // directly available as a data object (only the algorithm is available).    
  10. context = new Context (new ConcreteStrategyA());    
  11. context.contextInterface();
  12.  
  13. // Strategy can be reused instead of creating a new instance every time it is used.
  14. // Sort example
  15. MergeSort mergeSort = new MergeSort();
  16. QuickSort quickSort = new QuickSort();
  17. ...
  18. context = new Context (mergeSort);
  19. context.Sort();
  20. ...
  21. context = new Context (quickSort);
  22. context.Sort();
  23. ...
  24. context = new Context (mergeSort);
  25. context.Sort();
  26.      
  27. class Sorter abstract
  28. {
  29.    virtual void Sort() = 0;
  30. }
  31.  
  32. // MergeSorter IS A Sorter
  33. class MergeSorter : public Sorter
  34. {
  35.    virtual void Sort() override;
  36. }
  37.      
  38. class SortStrategy abstract
  39. {
  40.    virtual void Sort() = 0;
  41. }
  42.  
  43. // Sorter HAS A SortStrategy
  44. class Sorter
  45. {
  46.    Sorter(SortStragety *strategy) : mStrat(strategy) {}
  47.  
  48.    void Sort() {mStrat->Sort();}
  49.  
  50.    SortStrategy *mStrat;
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top