JavaScript 14 Strategy Patterns of 23 Design Patterns

Concept and characteristics

Concept:
Strategy mode is a kind of encapsulation of multiple conditional statements, defining a series of algorithms, and encapsulating each algorithm so that they can be replaced with each other. Changes in the algorithm will not affect customers who use the algorithm.

Features:

  1. Strategy mode can avoid the use of multiple conditional statements.
  2. Strategy mode provides a series of algorithm families that can be reused.
  3. The strategy pattern provides different implementations of the same behavior.
  4. The strategy mode puts the use of the algorithm in the environment, and the realization of the algorithm is put in different specific strategies.
  5. The strategy mode will result in many specific strategy classes.

Structure and realization

Strategy mode includes abstract strategy, concrete strategy and environment.
Abstract strategy: Define a public interface. Different algorithms implement this interface in different ways. Environment classes use this interface to call different algorithms.
Specific strategy: implements the interface defined by the abstract strategy and provides specific algorithm implementation.
Environment: Holding a reference to a strategy class, you can set or get a certain strategy class, and finally call it to the client.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>React App</title>
</head>
<body>
<script>
    //坐飞机出行-具体策略A
    class Fly{
    
    
        goOut(){
    
    
            console.log("坐飞机出来旅行,好开心");
        }
    }
    //做绿皮车出行-策略B
    class Train{
    
    
        goOut(){
    
    
            console.log("坐绿皮车出来旅行,不开心");
        }
    }
    //选择出行方式的人
    class Person{
    
    
        setOutWay(way){
    
    
            this.way = way;
        }
        goOut(){
    
    
            this.way.goOut();
        }
    }     
    class Customer {
    
    
        static  main() {
    
                
            let jack = new Person();
            jack.setOutWay(new Fly());//当然是选择坐飞机啦
            jack.goOut();
        }
    }
    Customer.main();
</script>
</body>
</html>

Application scenario

  1. When a system needs to dynamically select one of several algorithms, each algorithm can be encapsulated into a specific strategy.
  2. Multiple behaviors are defined in a class, and these behaviors appear in the form of multiple conditional statements in the operation of this class. Each branch can be transplanted to a specific strategy class instead of conditional statements.
  3. The algorithms in the system are completely independent, and the implementation details of specific algorithms are required to be hidden from customers.
  4. Multiple classes only differ in their performance behaviors. You can choose a strategy mode to dynamically select the specific behavior to be implemented at runtime.

Applications

Nothing.

to sum up

When encountering multiple-choice business scenarios, conditional statements are usually used. But when there are more and more conditions, the conditional statement will become bloated, difficult to maintain, and difficult to expand. At this time, using the strategy mode can well separate the use and implementation.

【Strategy】:
1. Provide corresponding business methods.

[Environmental category]:
1. Provide setStrategy method to set strategy category.
2. Provide business methods and call business methods of strategy.

Guess you like

Origin blog.csdn.net/weixin_44135121/article/details/106017494