策略模式(Strategy Pattern)的介绍

来源:互联网 发布:淘宝宝贝图片分辨率 编辑:程序博客网 时间:2024/06/11 07:15

In computer programming, the strategy pattern (also known as the policy pattern) is a particular software design pattern, whereby algorithms can be selected at runtime.

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.

This pattern is invisible in languages with first-class functions. See the Python, Scala or Perl implementations for examples.

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.

 

C# Strategy Pattern Example Code:

 

java Strategy Pattern Example Code:

 

c Strategy Pattern Example Code:

php Strategy Pattern Example Code:

 

原创粉丝点击