Design Patterns: Study Notes (13) - Strategy Mode

Design Patterns: Study Notes (13) - Strategy Mode

Business scene

  There are many ways to complete a task, each approach is a strategy that we sometimes need to complete the task of different options, depending on environmental conditions or strategies . To read the data, there are many algorithms can be completed the same function, such as from a file, cache, server, etc., but their solutions are different ways to improve the flexibility and scalability of the system, then you can use the strategy design mode enables the system flexibility to choose the solution.

  

  Strategy Mode (Strategy Pattern): the definition of a series of algorithms, each algorithm is a package together and make them interchangeable. Strategy mode allows algorithm independent of changes in the use of its customers , also known as policy mode (Policy).

Mode structure

  Strategy mode includes the following roles:

  • Context: Environmental, environmental policy is to call read.
  • Strategy: abstract class policy, the policy is read as shown above
  • ConcreteStrategy: concrete strategy class, is the image above to read from a file policy, read policy from the server, etc.

  

 

Timing diagram

  

  As can be seen on a map, in the environment we need to register policy class class, and calls the environment class read method, the specific strategy will perform the appropriate actions to achieve flexibility.

 

Pattern Analysis

 

 

Guess you like

Origin www.cnblogs.com/MrSaver/p/11387848.html