zoukankan      html  css  js  c++  java
  • 【编程思想】【设计模式】【行为模式Behavioral】模板模式Template

    Python转载版

    https://github.com/faif/python-patterns/blob/master/behavioral/template.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """
    http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
    
    An example of the Template pattern in Python
    
    *TL;DR80
    Defines the skeleton of an algorithm, deferring steps to subclasses.
    """
    
    ingredients = "spam eggs apple"
    line = '-' * 10
    
    
    # Skeletons
    def iter_elements(getter, action):
        """Template skeleton that iterates items"""
        for element in getter():
            action(element)
            print(line)
    
    
    def rev_elements(getter, action):
        """Template skeleton that iterates items in reverse order"""
        for element in getter()[::-1]:
            action(element)
            print(line)
    
    
    # Getters
    def get_list():
        return ingredients.split()
    
    
    def get_lists():
        return [list(x) for x in ingredients.split()]
    
    
    # Actions
    def print_item(item):
        print(item)
    
    
    def reverse_item(item):
        print(item[::-1])
    
    
    # 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)
                 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()
    
    ### OUTPUT ###
    # 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']
    # ----------
    Python转载版
  • 相关阅读:
    Eclipse设置智能提示
    【GoLang】golang 如何像Java 一样通过类名反射对象?
    【GoLang】golang 面向对象编程 & 面向接口编程
    【Nginx】nginx 代理 Haproxy 怎么设置?
    【GoLang】GoLang struct 使用
    【GoLang】50 个 Go 开发者常犯的错误
    【GoLang】与或非 异或操作
    【GoLang】GoLang UTF8 与 Unicode
    【GoLang】GoLang 错误处理 -- 异常处理思路示例
    【GoLang】GoLang 的流程与函数
  • 原文地址:https://www.cnblogs.com/demonzk/p/9035675.html
Copyright © 2011-2022 走看看