Java设计模式(4) -- 模板方法

本文详细介绍了模板方法设计模式的概念及应用方式,通过一个具体的文档处理应用案例展示了如何定义算法骨架,并允许子类重新定义算法步骤而不改变整体结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Template Method

 

英文简要描述

Intent
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

How to

AbstractClass
defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
ConcreteClass
implements the primitive operations to carry out subclass-specific steps of the algorithm.

Known cases
A lot

 

UML

模板方法通常和工厂方法一起使用,以下是样例:

上例的uml

primitive operation

抽象方法,子类必须实现

hook operation

钩子方法,有默认实现,子类可以重写其行为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值