The strategy pattern is the behavior pattern of an object and is intended to encapsulate a set of algorithms. Dynamically select the required algorithm and use it.
Strategy pattern refers to a pattern involving decision-making control in a program. The strategy pattern is very powerful because the core idea of this design pattern itself is the polymorphic idea of object-oriented programming.
Three roles of strategy mode:
1. abstract strategy role
2. Specific strategic roles
3. Environment roles (references to abstract policy roles)
Implementation steps:
1. Define abstract role classes (define common abstract methods for each implementation)
2. Define a specific strategy class (concretely implement the common method of the parent class)
3. Define environment role classes (privately declare abstract role variables, overload construction methods, and execute abstract methods)
Just outside the realm of programming, there are many examples of the Strategy Pattern. For example:
If I need to go to work from home in the morning, I can have several strategies to consider: I can take the subway, take the bus, walk or other ways. Each strategy achieves the same results but uses different resources.
All resources on this website are contributed and published by netizens, or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this website are for learning and reference only. Please do not use them for commercial purposes, otherwise you will be responsible for all consequences incurred! If there is any infringement, please contact us to delete and remove it. Contact information: admin@php.cn