FACTOID # 53: If you thought Antarctica was inhospitable, think again - its land area is only ninety-eight percent ice. Reassuringly, the other 2% is categorised as "barren rock".
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Strategy pattern

In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime. Computer programming (often shortened to programming or coding) is the process of writing, testing, and maintaining the source code of computer programs. ... In software engineering (or computer science), a design pattern is a general repeatable solution to a commonly occurring problem in software design. ... In mathematics, computing, linguistics, and related disciplines, an algorithm is a finite list of well-defined instructions for accomplishing some task that, given an initial state, will terminate in a defined end-state. ...


In some programming languages, such as those without polymorphism, the issues addressed by this pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax. In computer science, polymorphism means allowing a single definition to be used with different types of data (specifically, different classes of objects). ... In computer science, reflection is the process by which a computer program of the appropriate type can be modified in the process of being executed, in a manner that depends on abstract features of its code and its runtime behavior. ... It has been suggested that Software pointer be merged into this article or section. ... In object-oriented programming there are two notions of delegation. ...


The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them.

Contents

Diagram

Strategy Pattern Image File history File links This is a lossless scalable vector image. ...


Code Examples

C#

 using System; namespace Wikipedia.Patterns.Strategy { // MainApp test application class MainApp { static void Main() { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyA()); context.Execute(); context = new Context(new ConcreteStrategyB()); context.Execute(); context = new Context(new ConcreteStrategyC()); context.Execute(); } } // The classes that implement a concrete strategy should implement this // The context class uses this to call the concrete strategy interface IStrategy { void Execute(); } // Implements the algorithm using the strategy interface class ConcreteStrategyA : IStrategy { public void Execute() { Console.WriteLine( "Called ConcreteStrategyA.Execute()" ); } } class ConcreteStrategyB : IStrategy { public void Execute() { Console.WriteLine( "Called ConcreteStrategyB.Execute()" ); } } class ConcreteStrategyC : IStrategy { public void Execute() { Console.WriteLine( "Called ConcreteStrategyC.Execute()" ); } } // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object class Context { IStrategy strategy; // Constructor public Context(IStrategy strategy) { this.strategy = strategy; } public void Execute() { strategy.Execute(); } } } 

The title given to this article is incorrect due to technical limitations. ...

Strategy versus Bridge

The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure. In the field of software engineering, the Unified Modeling Language (UML) is a standardized specification language for object modeling. ... The bridge pattern is a design pattern used in software engineering which is meant to decouple an abstraction from its implementation so that the two can vary independently (Gamma et. ... The bridge pattern is a design pattern used in software engineering which is meant to decouple an abstraction from its implementation so that the two can vary independently (Gamma et. ...


The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.


Strategy Pattern and OCP

According to Strategy pattern, the behaviours of a class should not be inherited, instead they should be encapsulated using interfaces. As an example, consider a car class. Two possible behaviours of car are brake and accelerate.



Since accelerate and brake behaviours change frequently between models, a common approach is to implement these behaviours in subclasses. This approach has significant drawbacks: accelerate and brake behaviours must be declared in each new Car model. This may not be a concern when there are only a small number of models, but the work of managing these behaviors increases greatly as the number of models increases, and requires code to be duplicated across models. Additionally, it is not easy to determine the exact nature of the behavior for each model without investigating the code in each.



The strategy pattern uses composition instead of inheritance. In the strategy pattern behaviours are defined as separate interfaces and abstract classes that implement these interfaces. Specific classes encapsulate these interfaces. This allows better decoupling between the behaviour and the class that uses the behaviour. The behaviour can be changed without breaking the classes that use it, and the classes can switch between behaviours by changing the specific implementation used without requiring any significant code changes. Behaviours can also be changed at run-time as well as at design-time. For instance, a car object’s brake behaviour can be changed from BrakeWithABS() to Brake() by changing the brakeBehaviour member to:


brakeBehaviour = new Brake();



This gives greater flexibility in design and is in harmony with OCP (Open Closed Principle) that states classes should be open for extension but closed for modification.


See also

In object-oriented programming languages, a mixin is an approach to implementing classes that differs from the most widely-used approach coming from the programming language Simula. ... Policy-based design is a programming technique summarized as a compile-time equivalent of the Strategy pattern. ... In computer science, a programming language is said to support first-class functions if it treats functions as first-class objects. ... Template method: UML class diagram. ... The bridge pattern is a design pattern used in software engineering which is meant to decouple an abstraction from its implementation so that the two can vary independently (Gamma et. ... In object-oriented programming, the open/closed principle states that a class must be both open and closed, where open means it has the ability to be extended and closed means it cannot be modified other than by extension. ...

External links


  Results from FactBites:
 
Strategy pattern - Wikipedia, the free encyclopedia (113 words)
In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime depending on conditions, like strategies in a war situation.
The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.
The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.