Quantcast
Viewing all articles
Browse latest Browse all 10

Strategy

Description:

Strategy pattern  is a particular software design pattern, whereby algorithms can be selected at runtime. Formally speaking, the strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Benefits:

  • An alternative to subclassing
  •  Defines each behavior in its own class, which eliminates conditional statements
  •  Easier to extend a model to incorporate new behaviors without recoding the application

When to use:

  • Many related classes differ only in their behavior.
  • You need different variants of an algorithm.
  • An algorithm uses data unknown to clients.

Diagram:

Image may be NSFW.
Clik here to view.

Code sample:


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 10

Trending Articles