Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Strategy pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{short description|Software design pattern}} In [[computer programming]], the '''strategy pattern''' (also known as the '''policy pattern''') is a [[behavioral design pattern|behavioral]] [[design pattern (computer science)|software design pattern]] that enables selecting an [[algorithm]] at runtime. Instead of implementing a single algorithm directly, code receives runtime instructions as to which in a family of algorithms to use.<ref>{{cite web|title=The Strategy design pattern - Problem, Solution, and Applicability|url=http://w3sdesign.com/?gr=b09&ugr=proble|website=w3sDesign.com|access-date=2017-08-12}}</ref> Strategy lets the algorithm vary independently from clients that use it.<ref>Eric Freeman, Elisabeth Freeman, Kathy Sierra and Bert Bates, ''Head First Design Patterns'', First Edition, Chapter 1, Page 24, O'Reilly Media, Inc, 2004. {{ISBN|978-0-596-00712-6}}</ref> Strategy is one of the patterns included in the influential book ''[[Design Patterns]]'' by Gamma et al.<ref name="GoF">{{cite book|author=Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides|title=Design Patterns: Elements of Reusable Object-Oriented Software|year=1994|publisher=Addison Wesley|isbn=0-201-63361-2|pages=[https://archive.org/details/designpatternsel00gamm/page/315 315ff]|url-access=registration|url=https://archive.org/details/designpatternsel00gamm/page/315}}</ref> 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 runtime and may require radically different validation to be performed. The validation algorithms (strategies), encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without [[Duplicate code|code duplication]]. Typically, the strategy pattern stores a reference to code in a data structure and retrieves it. This can be achieved by mechanisms such as the native [[function pointer]], the [[first-class function]], classes or class instances in [[object-oriented programming]] languages, or accessing the language implementation's internal storage of code via [[Reflection (computer science)|reflection]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)