Detailed explanation of the strategy model of the basic design model of go language

Time:2022-1-19
catalogue
  • summary
    • Multiple treatment methods for the same type of problems
    • UML

  • summary
  • Example

Detailed explanation of the strategy model of the basic design model of go language

summary

Define a series of algorithms and encapsulate each algorithm. And let them replace each other. The policy pattern allows the algorithm to change independently of the customers who use it.

Multiple treatment methods for the same type of problems

1、 Do not use policy mode

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main
import "fmt"
type User struct {
    Name string
}
func (this User) travel(t string) {
    switch t {
    Case "aircraft":
        fmt. Printf ("% s, air travel \ n", this. Name)
    Case "train":
        fmt. Printf ("% s, train travel \ n", this. Name)
    Case "walking":
        fmt. Printf ("% s, walking \ n", this. Name)
    default:
        fmt. Println ("didn't you choose the travel mode")
    }
}
func main() {
    User {"Zhang San"} Travel ("aircraft")
    User {"Zhang San"} Travel ("train")
    User {"Zhang San"} Travel ("walk")
    User {"Zhang San"} travel("")
}

Question:

There are many and complex codes, such as if… Else… Many, which are not conducive to maintenance and expansion, and violate the “opening and closing principle”. The source code must be modified to add new travel modes. The reusability is poor, and one or some of the algorithms cannot be reused alone

2、 Strategy mode

Example of life strategy mode:

Zhang San went to Beijing from Guangdong [1. Take a plane, 2. Take a train, 3. Walk] the goose factory launched three kinds of members, namely members, super members and gold medal members [different skin, different discounts] Zhuge Liang’s clever plan

The policy pattern involves three roles:

number role describe
1 Context role Hold a reference to strategy
2 Abstract strategy role This is an abstract role, usually implemented by an interface or abstract class. This role gives the interfaces required for all specific policy classes
3 Concrete strategy role The related algorithms or behaviors are wrapped.

UML

Detailed explanation of the strategy model of the basic design model of go language

summary

advantage:

The policy mode provides perfect support for the “opening and closing principle”. Users can choose algorithms or behaviors without modifying the original system, or flexibly – add new algorithms or behaviors. Provides a way to manage related algorithm families to replace inheritance relationships. Avoid using multiple conditional transition statements.

Disadvantages:

The client must know all policy classes and decide which policy class to use. The policy mode will result in many policy classes. The number of objects can be reduced to a certain extent by using the meta mode

Example

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import "fmt"
/*Travel mode*/
type Itravel interface {
    Travel()
}
/*Aircraft*/
type Aircraft struct{}
/*Train*/
type Train struct{}
/*Walk*/
type Walk struct{}
/*Specific strategy category 1: air travel*/
func (this Aircraft) Travel() {
    fmt. Println ("air travel")
}
/*Specific strategy category 2: train travel*/
func (this Train) Travel() {
    fmt. Println ("train travel")
}
/*Specific strategy category 3: walking*/
func (this Walk) Travel() {
    fmt. Println ("walking")
}
/*Environment class*/
type User struct {
    Name    string
    Itravel Itravel
}
func (this User) travel() {
    fmt.Printf("%s", this.Name)
    this.Itravel.Travel()
}
func main() {
    User: = user {"Zhang San", aircraft {}
    user.travel()
    User = user {"Li Si", new (train)}
    user.travel()
    User = user {"king 5", & walk {}
    user.travel()
}

The above is the detailed content of the strategy mode example of the basic design mode of go language. For more information about the strategy mode of go language, please pay attention to other relevant articles of developeppaper!