page_title: Strategy pattern
section_title: UML class and sequence diagram
hierarchical_section_title: Strategy pattern / Structure / UML class and sequence diagram
caption_attribution_description: English: Strategy design pattern: Sample UML class and sequence diagram.
context_page_description: In computer programming, the strategy pattern is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
  Strategy lets the algorithm vary independently from clients that use it. Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al. that popularized the concept of using design patterns to describe how to design flexible and reusable object-oriented software. Deferring the decision about which algorithm to use until runtime allows the calling code to be more flexible and reusable.
  For instance, a class that performs validation on incoming data may use the strategy pattern to select a validation algorithm depending on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known until run-time and may require radically different validation to be performed.
context_section_description: In the above UML class diagram, the Context class doesn't implement an algorithm directly.
  Instead, Context refers to the Strategy interface for performing an algorithm (strategy.algorithm()), which makes Context independent of how an algorithm is implemented.
  The Strategy1 and Strategy2 classes implement the Strategy interface, that is, implement (encapsulate) an algorithm.
  
  The UML sequence diagram
  shows the run-time interactions: The Context object delegates an algorithm to different Strategy objects. First, Context calls algorithm() on a Strategy1 object,
  which performs the algorithm and returns the result to Context. 
  Thereafter, Context changes its strategy and calls algorithm() on a Strategy2 object,
  which performs the algorithm and returns the result to Context.
caption: A sample UML class and sequence diagram for the Strategy design pattern. [4]
exif: {}