Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is a strategic model?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Today, I would like to talk to you about what is a strategic model, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can gain something according to this article.

As a kind of software design pattern, policy pattern means that the object has a certain behavior, but in different scenarios, the behavior has different implementation algorithms. This pattern solves the complexity and difficulty of using "if...else" when there are many similar algorithms.

Introduction

Intention: define a series of algorithms, encapsulate them one by one, and make them interchangeable.

Main solution: when there are many similar algorithms, the complexity and difficulty of using if...else are difficult to maintain.

When to use: there are many classes in a system, and what distinguishes them is their direct behavior.

How to solve it: encapsulate these algorithms into a class and replace them arbitrarily.

Key code: implement the same interface.

Application examples: 1. Zhuge Liang's ingenious plan, each bag is a strategy. 2, the way to travel, choose to ride a bike, take a car, each way of travel is a strategy. 3. LayoutManager in JAVA AWT.

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

Disadvantages: 1. The number of policy classes will increase. 2. All policy classes need to be exposed.

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

Note: if a system has more than four policies, you need to consider using a mixed mode to solve the problem of inflated policy classes.

Realize

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

StrategyPatternDemo, our demo class uses Context and policy objects to demonstrate how Context behaves when the policy it configures or uses changes.

After reading the above, do you have any further understanding of what the strategic model is? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report