Design pattern understanding: template method

The template method belongs to the class behavior pattern, usually defines a stable skeleton, and the realization of specific content is delayed to the subclass.

The realization idea of ​​the template method: realize the stable part, and open the part that is easy to change to actual users.

Common examples of template methods: inheritance, virtual functions, etc.

For example, in JAVA, like using threads to implement the download function, you only need to define a thread to implement the run method. For Library developers, there is no need to implement threads with different functions, and the implementation of threads is left to the developers of actual applications. Library developers only need to implement stable operations such as thread system scheduling, which is "to achieve stability and open change". Of course, for application developers, it is necessary to implement the "changed part" by themselves, and the realization logic of the functionally stable part is a "black box" for application developers. For the use of JAVA threads, developers only need to know that the download function is written in the run method to achieve the characteristics of "concurrency, sharing process resources" at the same time.

For example, in an application program that calculates the area of ​​a figure, there is already a function of calculating the area of ​​rectangles and triangles, and there is a demand for "adding the function of calculating the area of ​​a circle".

One way is to "define an independent class for each graphic", and the second is to "define a graphic abstract class, and let each graphic inherit the abstract class." "

---------- 实现方式1-------------
class rectangular{
    ....
 double area(){.....}
}
class triangle{
    ....
 double area(){.....}
}

int main(){
    
    if(type == "rectangular"){
         rectangular  r1;
         ...
         r1.area() ; 
    }else if(type =='triangle'){
         triangle  t1;
         ...
         t1.area();
    }
    return 0 ;
}

---------- 实现方式2-------------
class shape{
    ...
 double area()=0;
}
class rectangular:public shape{
    ...
 double area(){.....}
}
class triangle:public shape{
    ...
 double area(){.....}
}

int main(){
    rectangular  r1;
    ...
    r1.area() ; 
   
    return 0 ;
}

Obviously, the former approach faces this new requirement. Not only does it need to create a class entity, but the implementation process also needs to change the process in the main method according to the specific type. In the second method, the library developer is responsible for the realization of the definition of the shape class and the realization of the main method. The actual application developer only needs to create a class that inherits the shape to complete this requirement.

The template method is mainly concerned with "change" and "stability". Separating the two and extracting the changed part is the key to the idea of ​​this template method.

Of course, if every step in the functional skeleton is changed or stable from beginning to end, then don't use the template method.

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/113957043