Home > Common Problem > body text

What is the strategy pattern?

Guanhui
Release: 2020-06-28 16:50:48
Original
4255 people have browsed it

As a software design pattern, the strategy pattern means that the object has a certain behavior, but in different scenarios, the behavior has different implementation algorithms. This pattern solves the problem of multiple similar algorithms. Below, the complexity and difficulty of maintenance caused by using "if...else".

What is the strategy pattern?

Introduction

Intent: Define a series of algorithms, encapsulate them one by one, and make them accessible Replace each other.

Main solution: When there are multiple similar algorithms, using if...else brings complexity and difficulty in maintenance.

When to use: A system has many, many classes, and what distinguishes them is their direct behavior.

How to solve: Encapsulate these algorithms into classes one by one and replace them arbitrarily.

Key code: implement the same interface.

Application examples: 1. Zhuge Liang’s tips, each tip is a strategy. 2. How to travel, choose riding a bicycle or taking a car. Each way of traveling is a strategy. 3. LayoutManager in JAVA AWT.

Advantages: 1. The algorithm can be switched freely. 2. Avoid using multiple conditional judgments. 3. Good scalability.

Disadvantages: 1. Strategy categories will increase. 2. All strategy classes need to be exposed to the outside world.

Usage scenarios: 1. If there are many classes in a system, and the difference between them is only their behavior, then the strategy pattern can be used to dynamically let an object choose one behavior among many behaviors. 2. A system needs to dynamically choose one of several algorithms. 3. If an object has many behaviors, without appropriate patterns, these behaviors have to be implemented using multiple conditional selection statements.

Note: If a system has more than four strategies, you need to consider using mixed mode to solve the problem of policy class expansion.

Implementation

We will create a Strategy interface that defines the activity and an entity strategy class that implements the Strategy interface. Context is a class that uses a certain strategy.

StrategyPatternDemo, our demo class uses Context and strategy objects to demonstrate the behavior changes of Context when the strategy it is configured or used changes.

What is the strategy pattern?

Recommended tutorial: "PHP"

The above is the detailed content of What is the strategy pattern?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!