Unity game design pattern - Strategy pattern (Strategy)

1. Reasons to use the strategy pattern

When designing game character damage, different calculation formulas are used due to differences in characters. How to solve problems such as distributing the same calculation method to different characters, adding new characters and increasing maintenance can be solved by using the strategy mode.

Second, the definition of strategy mode

GoF's interpretation of the strategy pattern: define a set of algorithms, and encapsulate each algorithm so that they can be used interchangeably with each other. The strategy pattern makes these algorithms more independent when clients use them.

When dealing with the so-called "algorithms", the strategy mode focuses on the details, and ignores the calculation methods and rules.

3. Instructions for using the strategy mode

insert image description here
Participant Instructions:

  • Strategy (Strategy interface class): Provides methods that can be used by the "Strategy Client".
  • ConcreteStrategyA~C (Strategy Implementation Class): Implementation of different algorithms.
  • Context (strategy client): owns an object reference of the Strategy class, and obtains the desired calculation result through the object reference.

Fourth, the implementation example of the strategy pattern

The shared interface of the algorithm:

 	/// <summary>
    /// 算法的共享接口
    /// </summary>
    public abstract class Strategy
    {
    
    
        public abstract void AlgorithmInterface();
    }

Implement various algorithms:

   /*  各类算法的实现 */

    /// <summary>
    /// 算法 A
    /// </summary>
    public class ConcreteStrategyA : Strategy
    {
    
    
        public override void AlgorithmInterface()
        {
    
    
            //print
        }
    }
    /// <summary>
    /// 算法 B
    /// </summary>
    public class ConcreteStrategyB : Strategy
    {
    
    
        public override void AlgorithmInterface()
        {
    
    
            //print
        }
    }
    /// <summary>
    /// 算法 C
    /// </summary>
    public class ConcreteStrategyC : Strategy
    {
    
    
        public override void AlgorithmInterface()
        {
    
    
            //print
        }
    }

The client that owns the Strategy object

	/// <summary>
    /// 拥有Strategy 对象的客户端
    /// </summary>
    public class Context
    {
    
    
        Strategy strategy = null;

        /// <summary>
        /// 设置算法
        /// </summary>
        /// <param name="strategy"></param>
        public void SetStrategy(Strategy strategy)
        {
    
    
            strategy = strategy;
        }
        /// <summary>
        ///执行当前的算法
        /// </summary>
        public void ContextInterface()
        {
    
    
            strategy.AlgorithmInterface();
        }
    }

V. Advantages and Precautions of the Intermediary Model

advantage:

  1. Make character attributes easy to maintain;
  2. It is not necessary to write program code for the role type;
  3. The replacement of the calculation formula is more convenient;

shortcoming:

  1. The parameter setting when calculating the formula requires the outside world to provide relevant information as the calculation basis;
  2. The difference from the state pattern:
    insert image description here
    they are all classified under the category of Behavior Patterns, and a Context class maintains object references.
    difference:
    1. State is switched in a group of states, and there is a correspondence and connection relationship between states, while Strategy is composed of a group of classes that have no relationship and do not know each other's existence.
    2. State is limited by the switching rules of the state machine. It is possible to define all states at the beginning of the design, and add them later. If you want to join, you are limited by the existing state association. Strategy is a design pattern formed by encapsulating computing algorithms. , there is no dependency between the algorithms and can be added at any time.

6. Summary of Strategy Patterns

The complex calculation formula is separated from the client and called a group. After that, the client can decide the calculation formula strategy to be used according to the situation, which not only improves the flexibility of the system application, but also strengthens the maintenance of all calculation strategies in the system. Way.

Subsequent developers can easily find differences in related calculation formulas, and at the same time, the modification point will be narrowed down to the calculation formula itself, and it will not affect the client used.

Guess you like

Origin blog.csdn.net/qq_40120946/article/details/122530981