Sponsored Links

Rabu, 16 Mei 2018

Sponsored Links

What is the difference between the template method and the ...
src: i.stack.imgur.com

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 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. 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 some 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.


Video Strategy pattern



Structure

UML class and sequence diagram

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.

Class diagram


Maps Strategy pattern



Example

C#

The following example is in C#.

Java

The following example is in Java.


JAVA EE: Strategy Design pattern - Implementation [Payment]
src: 2.bp.blogspot.com


Strategy and open/closed principle

According to the strategy pattern, the behaviors of a class should not be inherited. Instead they should be encapsulated using interfaces. This is compatible with the open/closed principle (OCP), which proposes that classes should be open for extension but closed for modification.

As an example, consider a car class. Two possible functionalities for car are brake and accelerate. Since accelerate and brake behaviors change frequently between models, a common approach is to implement these behaviors in subclasses. This approach has significant drawbacks: accelerate and brake behaviors must be declared in each new Car model. 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, behaviors are defined as separate interfaces and specific classes that implement these interfaces. This allows better decoupling between the behavior and the class that uses the behavior. The behavior can be changed without breaking the classes that use it, and the classes can switch between behaviors by changing the specific implementation used without requiring any significant code changes. Behaviors can also be changed at run-time as well as at design-time. For instance, a car object's brake behavior can be changed from BrakeWithABS() to Brake() by changing the brakeBehavior member to:


JAVA EE: Strategy Design pattern - Implementation [Sort]
src: 4.bp.blogspot.com


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

What is the difference between the template method and the ...
src: i.stack.imgur.com


References


JAVA EE: Strategy Design pattern - Sequence Diagram
src: 1.bp.blogspot.com


External links

  • Strategy Pattern in UML (Spanish, but english model)
  • The Strategy Pattern from the Net Objectives Repository
  • Strategy Pattern for Java article
  • Strategy Pattern for CSharp article
  • Strategy Pattern for C article
  • Strategy pattern in UML and in LePUS3 (a formal modelling notation)
  • Refactoring: Replace Type Code with State/Strategy
  • Implementation of the Strategy pattern in JavaScript

Source of the article : Wikipedia

Comments
0 Comments