encyclopegia.org

Strategy pattern

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral 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. 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 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 code duplication. Typically, the strategy pattern stores a reference to code in a data structure and retrieves it.…

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral 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. 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 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 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. Brake* brakeBehavior = new Brake(); package org.wikipedia.examples; /* Encapsulated family of Algorithms * Interface and its implementations */ interface IBrakeBehavior { public void brake(); } class BrakeWithABS implements IBrakeBehavior { public void brake() { System.out.println("Brake with ABS applied"); } } class Brake implements IBrakeBehavior { public void brake() { System.out.println("Simple Brake applied"); } } // Client that can use the algorithms above interchangeably abstract class Car { private IBrakeBehavior brakeBehavior; public Car(IBrakeBehavior brakeBehavior) { this.brakeBehavior = brakeBehavior; } public void applyBrake() { brakeBehavior.brake(); } public void setBrakeBehavior(IBrakeBehavior brakeType) { this.brakeBehavior = brakeType; } } // Client 1 uses one algorithm (Brake) in the constructor class Sedan extends Car { public Sedan() { super(new Brake()); } } // Client 2 uses another algorithm (BrakeWithABS) in the constructor class SUV extends Car { public SUV() { super(new BrakeWithABS()); } } // Using the Car example public class CarExample { public static void main(String[] arguments) { Car sedanCar = new Sedan(); sedanCar.applyBrake(); // This will invoke class "Brake" Car suvCar = new SUV(); suvCar.applyBrake(); // This will invoke class "BrakeWithABS" // set brake behavior dynamically suvCar.setBrakeBehavior(new Brake()); suvCar.applyBrake(); // This will invoke class "Brake" } } ==See also== Dependency injection Higher-order function List of object-oriented programming terms Mixin Policy-based design Type class Entity–component–system Composition over inheritance ==References== ==External links== Strategy Pattern in UML Strategy Pattern for C article Refactoring: Replace Type Code with State/Strategy Implementation of the Strategy pattern in JavaScript

References

No reference rows published for this article.

Cite this article

MLA

"Strategy pattern." encyclopegia.org, 2025, https://encyclopegia.org/wiki/strategy-pattern. Accessed 24 November 2026.

Chicago

"Strategy pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/strategy-pattern.

APA

encyclopegia.org. (2025). Strategy pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/strategy-pattern

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Time Bandits

enwiki · references available

Battle of Bautzen (1813)

enwiki · references available

Battle of Dresden

enwiki · references available

Down (band)

enwiki · references available

Glass fiber

enwiki · references available

Peter the Hermit

enwiki · references available

Multiple instruction, multiple data

enwiki · references available

Anagni

enwiki · references available

Marcel Cerdan

enwiki · references available

Charlton Comics

enwiki · references available

Ramsey theory

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1316974000 · 0.5 · snapshot enwiki-001316974000

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.