python: Template Method Pattern

用珠宝制作来类比再合适不过:

  • 所有珠宝(戒指、项链、手镯)的制作都有固定通用流程(算法骨架):选材 → 塑形 → 打磨 → 镶嵌 → 质检 → 成品
  • 其中,选材、打磨、质检 是通用步骤(所有珠宝都差不多)
  • 而 塑形、镶嵌 是可变步骤(戒指要塑成环形,项链要塑成链状;戒指镶嵌单颗主石,项链镶嵌多颗碎钻)
python 复制代码
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Template Method Pattern
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/22 10:20
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : JewelryMaker.py
# explain   : 学习
 
from abc import ABC, abstractmethod
 
 
class JewelryMaker(ABC):
    """
    抽象基类:定义珠宝制作的模板方法(算法骨架)
    """
 
    def make_jewelry(self):
        """
        模板方法:定义珠宝制作的整体流程(不允许子类重写,Python中用命名约定_开头或注释提醒)
        """
 
        self.select_material()  # 步骤1:选材(通用)
        self.shape()            # 步骤2:塑形(可变,子类实现)
        self.polish()           # 步骤3:打磨(通用)
        self.inlay()            # 步骤4:镶嵌(可变,子类实现)
        self.quality_check()    # 步骤5:质检(通用)
        print(f"【成品】{self.get_jewelry_type()}制作完成!\n")
 
 
    def select_material(self):
        """
        通用方法:所有珠宝都用相同的选材逻辑
        """
        print("1. 选材:选用925银+天然锆石")
 
 
    def polish(self):
        """
        通用方法:所有珠宝都用相同的打磨逻辑
        """
        print("3. 打磨:精细抛光至镜面效果")
 
 
    def quality_check(self):
        """
        通用方法:所有珠宝都用相同的质检逻辑
        """
        print("5. 质检:检查表面光滑度、镶嵌牢固度")
 
 
    @abstractmethod
    def shape(self):
        """
        抽象方法:塑形(子类必须实现)
        """
        pass
 
 
    @abstractmethod
    def inlay(self):
        """
        抽象方法:镶嵌(子类必须实现)
        """
        pass
 
 
    @abstractmethod
    def get_jewelry_type(self):
        """
        辅助方法:获取珠宝类型(子类实现)
        """
        pass
python 复制代码
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Template Method Pattern
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/22 10:23
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : RingMaker.py
# explain   : 学习
 
from TemplateMethodPattern.JewelryMaker import JewelryMaker
 
 
class RingMaker(JewelryMaker):
    """
    子类1:戒指制作(实现可变步骤)
    """
    def select_material(self):
        """
        重写 选材
        """
        print("1. 选材:将k999黄金锻造成环形,内径调整为28mm")
 
    def shape(self):
        """
        塑形
        """
        print("2. 塑形:将黄金料锻造成环形,内径调整为28mm")
 
    def inlay(self):
        """
        镶嵌
        """
        print("4. 镶嵌:在戒指中心镶嵌1颗5mm圆形锆石")
 
    def get_jewelry_type(self):
        """
 
        """
        return "戒指"
 
 
 
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述: Template Method Pattern
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/22 10:30
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : NecklaceMaker.py
# explain   : 学习
 
from TemplateMethodPattern.JewelryMaker import JewelryMaker
 
 
class NecklaceMaker(JewelryMaker):
    """
    子类2:项链制作(实现可变步骤)
    """
    def shape(self):
        """
        塑形
        """
        print("2. 塑形:将银料拉成细链,编织成十字链样式,长度45cm")
 
    def inlay(self):
        """
        镶嵌
        """
        print("4. 镶嵌:在项链吊坠处镶嵌3颗2mm碎钻,链条间隔镶嵌小锆石")
 
    def get_jewelry_type(self):
        """
        项链
        """
        return "项链"
python 复制代码
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/22 10:33
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : TemplateMethodBll.py
# explain   : 学习
from TemplateMethodPattern.RingMaker import RingMaker
from TemplateMethodPattern.NecklaceMaker import NecklaceMaker
 
 
class TemplateMethodBll(object):
    """
 
    """
    def demo(self):
        """
 
        """
        # 制作戒指
        ring_maker = RingMaker()
        print("=== 开始制作戒指 ===")
        ring_maker.make_jewelry()
 
        # 制作项链
        necklace_maker = NecklaceMaker()
        print("=== 开始制作项链 ===")
        necklace_maker.make_jewelry()

调用:

python 复制代码
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2023.1 python 3.11
# OS        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  oracle 21c Neo4j
# Datetime  : 2026/2/18 20:58
# User      : geovindu
# Product   : PyCharm
# Project   : pydesginpattern
# File      : main.py
# explain   : 学习
from bll.MementoBll import MementoBll
from bll.CommandBll import CommandBll
from bll.StateBll import StateBll
from bll.TemplateMethodBll import TemplateMethodBll
 
if __name__ == '__main__':
 
    #实现备忘录模式(Memento Pattern)
    #mementobll= MementoBll()
    #mementobll.demo()
    #命令模式(Command Pattern)
    #commandBll= CommandBll()
    #commandBll.demo()
    # 状态模式 State Pattern
    #stateBll =StateBll()
    #stateBll.demo()
    # 模板方法模式 Template Method Pattern
    templateMethodBll= TemplateMethodBll()
    templateMethodBll.demo()
 
    print('PyCharm')

输出:

相关推荐
IT毕设梦工厂17 分钟前
计算机毕业设计选题推荐:基于大数据的房地产交易数据分析与可视化|毕业设计选题|计算机毕设|选题推荐|毕设指导|项目定制|源码|高质量项目
大数据·hive·hadoop·python·数据分析·spark·课程设计
Wx-bishekaifayuan22 分钟前
springboot生活商城系统21035-计算机课程设计、毕业设计
spring boot·后端·python·spring·elasticsearch·golang·课程设计
FYKJ_201033 分钟前
django药饮个性化推荐系统54839-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
天赐范式40 分钟前
天赐范式第175天:让活着的成员互相验证——异质互验与重放反伪造
python·数字生命·天赐范式·动态运行时·互验·重放反伪造
Wx-bishekaifayuan42 分钟前
springboot贵州旅游系统55916-计算机课程设计、毕业设计
spring boot·后端·python·django·课程设计·express·旅游
计算机源码社1 小时前
【27届大数据毕设】基于Hadoop的跨境二手车价格对比可视化分析系统-基于K-Means与FPGrowth的二手车价格影响因素挖掘研究
大数据·hadoop·python·数据分析·spark·毕业设计·数据可视化
Fruit_Caller1 小时前
static
开发语言·c++
Thneonl6 小时前
Celery 生产踩坑:1000 任务积压与 acks_late 双重执行
后端·python
清桔6 小时前
模型的调用
python
小小张说故事6 小时前
Python 多线程为什么跑不快?asyncio 入门指南:异步并发从零上手
后端·python