Table of Contents
Strategy Pattern
Intent
Question
Solution
Real World Analogy
Strategy pattern structure
Pseudocode
Strategy mode is suitable for application scenarios
Implementation
Strategy Pattern Advantages and Disadvantages
Relationship to Other Patterns
Home Java javaTutorial What is the strategy pattern of java design patterns?

What is the strategy pattern of java design patterns?

Apr 29, 2023 am 11:43 AM
java

    Strategy Pattern

    Also known as: Strategy

    Intent

    Strategy Pattern is a A behavioral design pattern that allows you to define a series of algorithms and put each algorithm into a separate class so that the algorithm objects can be interchanged.

    What is the strategy pattern of java design patterns?

    Question

    One day, you plan to create a tour guide program for tourists. The core functionality of the program is to provide beautiful maps to help users quickly orient themselves in any city.

    A new feature in the app that users are looking forward to is automatic route planning: They want to enter an address and see the fastest route to their destination on a map.

    The first version of the program can only plan road routes. People traveling by car are very happy with it. But obviously, not everyone drives on vacation. So you added the ability to plan walking routes in your next update. You've since added the ability to plan public transportation routes.

    And this is just the beginning. Soon you'll be planning routes for cyclists. After a while, you again have to plan your route to visit all the attractions in the city.

    What is the strategy pattern of java design patterns?

    The guide code will become very bloated.

    Although this application is very successful from a business point of view, its technical part can cause you a lot of headaches: every time a new route planning algorithm is added, the size of the main class in the tour guide application increases by one times. Finally at some point, you feel that you can no longer maintain this pile of code.

    Whether it’s fixing a simple flaw or fine-tuning street weights, any modification to an algorithm affects the entire class, increasing the risk of introducing bugs into already functioning code.

    In addition, teamwork will become inefficient. If you recruit team members after the app is successfully released, they will complain that too much time is spent working on merge conflicts. In the process of implementing new features, your team needs to modify the same huge class, so the code they write may conflict with each other.

    Solution

    The Strategy pattern suggests identifying the classes responsible for accomplishing a specific task in many different ways, and then extracting the algorithms within them into a set of independent classes called strategies.

    The original class named context must contain a member variable to store a reference to each strategy. The context does not perform the task, but delegates the work to the connected policy object.

    The context is not responsible for selecting the algorithm that meets the needs of the task - the client will pass the required strategy to the context. In fact, the context does not know much about the strategies, it interacts with all strategies through the same common interface, which only needs to expose a method to trigger the algorithm encapsulated in the selected strategy.

    Thus, context can be independent of specific policies. This allows you to add new algorithms or modify existing ones without changing the context code or other strategies.

    What is the strategy pattern of java design patterns?

    Route planning strategy.

    In a tour guide application, each route planning algorithm can be extracted into an independent class with only one build­Routegenerating route method. This method receives the start and end points as parameters and returns a collection of route midpoints.

    Even if the parameters passed to each route planning class are exactly the same, the routes created may be completely different. The main job of the main tour guide class is to render a series of halfway points on the map and does not care about how the algorithm is chosen. There is also a method in this class for switching the current path planning strategy, so the client (such as a button in the user interface) can replace the currently selected path planning behavior with another strategy.

    Real World Analogy

    What is the strategy pattern of java design patterns?

    Various Travel Strategies to the Airport

    If you need to go to the airport. You can choose to take a bus, book a taxi or ride a bike. These are your travel strategies. You can choose one of these strategies based on factors like budget or time.

    Strategy pattern structure

    What is the strategy pattern of java design patterns?

    • Context (Context) maintains a reference to a specific strategy and is only passed The policy interface communicates with this object.

    • Strategy (Strategy) interface is a common interface for all specific strategies. It declares a context method for executing the strategy.

    • Concrete Strategies (Concrete Strategies) implement different variations of the algorithm used by the context.

    • When the context needs to run an algorithm, it calls the execute method on its connected policy object. The context is unclear as to the type of strategy involved and how the algorithm is executed.

    • Client (Client) will create a specific policy object and pass it to the context. The context provides a setter so that the client can override the associated policy at runtime.

    Pseudocode

    In this example, the context uses multiple strategies to perform different calculation operations.

    // 策略接口声明了某个算法各个不同版本间所共有的操作。上下文会使用该接口来
    // 调用有具体策略定义的算法。
    interface Strategy is
        method execute(a, b)
    // 具体策略会在遵循策略基础接口的情况下实现算法。该接口实现了它们在上下文
    // 中的互换性。
    class ConcreteStrategyAdd implements Strategy is
        method execute(a, b) is
            return a + b
    class ConcreteStrategySubtract implements Strategy is
        method execute(a, b) is
            return a - b
    class ConcreteStrategyMultiply implements Strategy is
        method execute(a, b) is
            return a * b
    // 上下文定义了客户端关注的接口。
    class Context is
        // 上下文会维护指向某个策略对象的引用。上下文不知晓策略的具体类。上下
        // 文必须通过策略接口来与所有策略进行交互。
        private strategy: Strategy
        // 上下文通常会通过构造函数来接收策略对象,同时还提供设置器以便在运行
        // 时切换策略。
        method setStrategy(Strategy strategy) is
            this.strategy = strategy
        // 上下文会将一些工作委派给策略对象,而不是自行实现不同版本的算法。
        method executeStrategy(int a, int b) is
            return strategy.execute(a, b)
    // 客户端代码会选择具体策略并将其传递给上下文。客户端必须知晓策略之间的差
    // 异,才能做出正确的选择。
    class ExampleApplication is
        method main() is
            创建上下文对象。
            读取第一个数。
            读取最后一个数。
            从用户输入中读取期望进行的行为。
            if (action == addition) then
                context.setStrategy(new ConcreteStrategyAdd())
            if (action == subtraction) then
                context.setStrategy(new ConcreteStrategySubtract())
            if (action == multiplication) then
                context.setStrategy(new ConcreteStrategyMultiply())
            result = context.executeStrategy(First number, Second number)
            打印结果。
    Copy after login

    Strategy mode is suitable for application scenarios

    When you want to use various algorithm variants in the object and want to be able to switch algorithms at runtime, you can use the strategy mode.

    The Strategy pattern allows you to indirectly change object behavior at runtime by associating objects to different sub-objects that can perform specific sub-tasks in different ways.

    Use the Strategy pattern when you have many similar classes that differ only slightly in how they perform certain behaviors.

    The Strategy pattern allows you to extract different behaviors into a separate class hierarchy and combine the original classes into the same one, thereby reducing duplicate code.

    If the algorithm is not particularly important in the logic of the context, using this pattern can isolate the business logic of the class from its algorithm implementation details.

    The Strategy pattern allows you to isolate the code, internal data, and dependencies of various algorithms from other code. Different clients can execute algorithms through a simple interface and can be switched at runtime.

    This pattern can be used when complex conditional operators are used in the class to switch between different variants of the same algorithm.

    Strategy pattern extracts all algorithms inherited from the same interface into independent classes, so conditional statements are no longer needed. The primitive object does not implement all variants of the algorithm, but instead delegates execution to one of the individual algorithm objects.

    Implementation

    • Find out the algorithm with a higher modification frequency from the context class (it may also be a complex condition used to select an algorithm variant at runtime operator).

    • Declares a common strategy interface for all variants of this algorithm.

    • Extract the algorithms into their respective classes one by one, and they must all implement the strategy interface.

    • Add a member variable in the context class to save a reference to the policy object. Then provide a setter to modify the member variable. The context can only interact with the policy object through the policy interface. If necessary, an interface can be defined to allow the policy to access its data.

    • The client must associate the context class with the corresponding policy so that the context can complete its main job in the expected manner.

    Strategy Pattern Advantages and Disadvantages

    • You can switch algorithms within an object at runtime.

    • You can isolate the implementation of the algorithm from the code that uses the algorithm.

    • You can use composition instead of inheritance.

    • Opening and closing principle. You can introduce new strategies without modifying the context.

    Strategy Pattern Advantages and Disadvantages

    • If your algorithm rarely changes, there is no reason to introduce new classes and interfaces. Using this pattern will only make the program overly complex.

    • The client must be aware of the differences between strategies - it needs to choose the appropriate strategy.

    • Many modern programming languages ​​support the function type feature, allowing you to implement different versions of an algorithm in a set of anonymous functions. This way, you use these functions in exactly the same way as using the policy object, without having to resort to additional classes and interfaces to keep your code simple.

    Relationship to Other Patterns

    The interfaces of the Bridge, Stateful, and Strategy (and to some extent Adapter) patterns are very similar. In fact, they are all based on the composition pattern-that is, delegating work to other objects, but each solves different problems. Patterns aren't just recipes for organizing code in a specific way; you can also use them to discuss the problems they solve with other developers.

    Command patterns and strategies look similar because both can parameterize objects with certain behaviors. However, their intentions are very different.

    • You can use commands to convert any operation into an object. The parameters of the operation will become member variables of the object. You can use transformations to delay the execution of operations, queue operations, save historical commands, or send commands to remote services.

    • Strategies, on the other hand, can often be used to describe different ways of accomplishing something, allowing you to switch algorithms within the same context class.

    Decoration mode allows you to change the appearance of an object, while strategies allow you to change its essence.

    The template method pattern is based on the inheritance mechanism: it allows you to change part of the algorithm by extending part of the content in the subclass. Strategies are based on a composition mechanism: you can change part of an object's behavior by providing different strategies for the corresponding behavior. Template methods operate at the class level, so it is static. Policies operate at the object level, thus allowing behavior to be switched at runtime.

    State can be thought of as an extension of strategy. Both are based on composition mechanisms: they both change its behavior in different situations by delegating part of the work to "helper" objects. The policy makes these objects completely independent of each other, and they are unaware of the existence of other objects. However, the state pattern does not limit the dependencies between specific states and allows them to change their states in different scenarios.

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

    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1659
    14
    PHP Tutorial
    1258
    29
    C# Tutorial
    1232
    24
    Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

    Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

    PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

    PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

    PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

    PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

    PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

    PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

    PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

    PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

    Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

    Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

    PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

    The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

    See all articles