Design Patterns - Behavioral Patterns, Template Patterns (16)

This article is reproduced from: http://www.cnblogs.com/ydf0509/p/8527685.html Author: ydf0509 Please indicate the statement when reprinting.

Template Method Pattern: Define the basic skeleton of a workflow or algorithm, and defer the implementation of certain steps to subclasses.

The template method pattern is the easiest pattern to summarize in object-oriented programming practice. Even if you don't know what a design pattern is at the beginning, you may have used this pattern often:

 

 

class Daili(object):
    def __init__(self,daili_name):
        self.daili_name = daili_name

    def extract(self):
         """ Extract proxy ip from website """ 
        pass   # #Return the list of proxy ip by subclass

    def check_daili(self, daili_list):
         print  ' detection ' +self.daili_name +   ' agent, save the available agents to redis '

    def collect_daili(self):
        daili_list = self.extract()
        self.check_daili(daili_list)


class XiciDaili(Daili):
     def extract(self):
         ''' Extract ip from XiciDaili proxy ''' 
        print  ' Extracting ' + self.daili_name + ' Website proxy ' 
        return [ ' some ip ' ]


class YaoYaoDaili(Daili):
     def extract(self):
         ''' Extract ip from Yaoyao proxy website ''' 
        print  ' Extracting ' + self.daili_name + ' Website proxy ' 
        return [ ' some ip ' ]   #


XiciDaili( ' Xici ' ) .collect_daili ()
YaoYaoDaili( ' Yaoyao ' ).collect_daili()

 

 

 

To achieve proxy ip collection, you may only write xici proxy extraction at the beginning, but then write yaoyao proxy extraction.

But the two classes, only the regular or css selector for extracting ip is different, and the other detection ip availability and saving proxy to redis are the same.

Therefore, it is very convenient to use the template mode. No matter how you collect the proxies of other websites, you only need to write your own extraction method, and you do not need to copy and paste the rest of the detection and storage.

 

 

2. The template mode is also very convenient and common to use in business processes. Subclasses only need to inherit and rewrite a small part. The skeleton is in the template class. If you modify the basic things, you can directly modify the template class, and one modification will take effect everywhere. If you use module plus function programming, you need to modify dozens or hundreds of files in batches.

oop is not only used in basic shared classes, but also in business process classes.

 

 3. Template mode and strategy mode can be replaced with each other.

If the above code is called a cast, and the following code is called a foot, the template mode is generally top-heavy, and the strategy mode is top-heavy. This is the general case. Subclasses only need to override a few methods, and some superclasses expose a lot of abstract methods, but only a few entities run the code.

 One is to write a large template class first, and then all subclasses override a method.
One is to write many strategy classes, and then use one class to combine these classes and call the methods of the strategy class.

template pattern 1 template parent class + n child class
strategy pattern n strategy class + 1 class that uses strategy

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156593&siteId=291194637