python Design Patterns - Template Mode

Foreword

Template model, this is more common in the development process, and its main function is to handle public class function declaration or in a template class, optimized for logic and reduce code.

1. What is the template pattern

As one popular example, you have a seal and affix his seal generally used red ink pad; one day into the hands of your son, he gave you painted a variety of colors, cough ... and then cover your face, red green and purple ... that the seal is a template, a variety of colors is different needs, the resulting color are different, but the essence is your name.
"Illustrates a design mode" is so defined template pattern: framework defines a processing flow in the parent class, specific processing modes implemented in a subclass.

2. longhand

If there are two processes, making coffee and tea

class MakeTea(object):
    def __init__(self, temperature, water):
        self.temperature = temperature
        self.water = water

    def add_water(self):
        print('add', self.water, 'water')

    def heat(self):
        print('heat water to', self.temperature)

    @staticmethod
    def add_tea():
        print('add tea')

    def make_tea(self):
        self.add_water()
        self.heat()
        self.add_tea()


class MakeCoffee(object):
    def __init__(self, temperature, water):
        self.temperature = temperature
        self.water = water

    def heat(self):
        print('heat water to', self.temperature)

    def add_water(self):
        print('add', self.water, 'water')

    @staticmethod
    def add_coffee():
        print('add coffee')

    def make_coffee(self):
        self.add_water()
        self.heat()
        self.add_coffee()

We are starting a process of tea:

if __name__ == '__main__':
    make_tea = MakeTea(100, '500ml')
    make_tea.make_tea()

Print Results:

add 500ml water
heat water to 100
add tea

We carefully analyze the above two processes classes, making coffee and tea have much in common, both heat and water process exactly the same two classes implement again, looks like a little code redundancy.

3. Template Mode

Here we define a MakeDrink class, this class is a template class MakeTea and MakeCoffee class to achieve a common add_water () and heat () method

class MakeDrink(object):
    def __init__(self, temperature, water):
        self.temperature = temperature
        self.water = water

    def add_water(self):
        print('add', self.water, 'water')

    def heat(self):
        print('heat water to', self.temperature)

    def make_drink(self):
        pass

Then on top of that we have inherited template to generate a new class of tea

class MakeCoffee(MakeDrink):
    def __init__(self, temperature, water):
        super(MakeCoffeeT, self).__init__(temperature, water)

    @staticmethod
    def add_coffee():
        print('add coffee')

    def make_drink(self):
        self.add_water()
        self.heat()
        self.add_coffee()

The new class, add a unique add_coffee () method, and implemented make_drink () method, a simple test

if __name__ == '__main__':
    make_coffee = MakeCoffee(85, '200ml')
    make_coffee.make_drink()

result:

add 200ml water
heat water to 85
add coffee

4.notes

Template mode more commonly used, the core idea is still inherited the idea, with the factory pattern, adapter pattern slightly similar.

Published 76 original articles · won praise 46 · views 110 000 +

Guess you like

Origin blog.csdn.net/qingquanyingyue/article/details/104076725