模板模式

ingredients = "spam eggs apple"
line = '-' * 10


# Skeletons
def iter_elements(getter, action):
    """Template skeleton that iterates items"""
    for element in getter():  # pos
        action(element)
        print(line)


def rev_elements(getter, action):
    """Template skeleton that iterates items in reverse order"""
    for element in getter()[::-1]:  # rev
        action(element)
        print(line)


# Getters
def get_list():
    return ingredients.split()  # ['spam','eggs','apple']


def get_lists():
    return [list(x) for x in ingredients.split()]


# [['s','p','a','m'],['e','g','g','s'],['a','p','p','l','e']]

# Actions
def print_item(item):
    print(item)  # pos


def reverse_item(item):
    print(item[::-1])  # rev


# Makes templates
def make_template(skeleton, getter, action):
    """Instantiate a template method with getter and action"""

    def template():
        skeleton(getter, action)

    return template


# Create our template functions
templates = [make_template(s, g, a)  # 笛卡尔积组合8种
             for g in (get_list, get_lists)
             for a in (print_item, reverse_item)
             for s in (iter_elements, rev_elements)]

# Execute them
for template in templates:
    template()

输出结果:

spam
----------
eggs
----------
apple
----------
apple
----------
eggs
----------
spam
----------
maps
----------
sgge
----------
elppa
----------
elppa
----------
sgge
----------
maps
----------
['s', 'p', 'a', 'm']
----------
['e', 'g', 'g', 's']
----------
['a', 'p', 'p', 'l', 'e']
----------
['a', 'p', 'p', 'l', 'e']
----------
['e', 'g', 'g', 's']
----------
['s', 'p', 'a', 'm']
----------
['m', 'a', 'p', 's']
----------
['s', 'g', 'g', 'e']
----------
['e', 'l', 'p', 'p', 'a']
----------
['e', 'l', 'p', 'p', 'a']
----------
['s', 'g', 'g', 'e']
----------
['m', 'a', 'p', 's']
----------

你可能感兴趣的:(模板模式)