行为型设计模式 - 模板方法 Template Method
作者:菜皮日记
- 2023-09-08 北京
本文字数:1774 字
阅读完需:约 6 分钟
简介
父类抽象类定义大的处理流程,部分细节做成抽象方法,留给子类去实现。
如 Java 的 JUnit 中, setUp tearDown 方法都是留给具体的测试用例来写,Servlet 中 service 处理了一个请求的大部分工作,留下 doGet 和 doPost 给业务自定义处理。
另外 callback 一般分两种方式:同步回调、异步回调,其中同步回调类似于模板方法模式,异步回调类似于观察者模式。
模板方法要基于继承,而回调更类似组合,
角色
抽象模板类
定义大的处理流程,留出部分方法给具体类实现
具体模板类
实现具体的处理逻辑
类图
如图,ConcreteClass 实现了一些具体逻辑
类图
代码
abstract class AbstractClass
{
final public function templateMethod(): void
{
$this->baseOperation1();
$this->requiredOperations1();
$this->baseOperation2();
$this->hook1();
$this->requiredOperation2();
$this->baseOperation3();
$this->hook2();
}
protected function baseOperation1(): void
{
echo "AbstractClass says: I am doing the bulk of the work\n";
}
protected function baseOperation2(): void
{
echo "AbstractClass says: But I let subclasses override some operations\n";
}
protected function baseOperation3(): void
{
echo "AbstractClass says: But I am doing the bulk of the work anyway\n";
}
abstract protected function requiredOperations1(): void;
abstract protected function requiredOperation2(): void;
protected function hook1(): void { }
protected function hook2(): void { }
}
class ConcreteClass1 extends AbstractClass
{
protected function requiredOperations1(): void
{
echo "ConcreteClass1 says: Implemented Operation1\n";
}
protected function requiredOperation2(): void
{
echo "ConcreteClass1 says: Implemented Operation2\n";
}
}
class ConcreteClass2 extends AbstractClass
{
protected function requiredOperations1(): void
{
echo "ConcreteClass2 says: Implemented Operation1\n";
}
protected function requiredOperation2(): void
{
echo "ConcreteClass2 says: Implemented Operation2\n";
}
protected function hook1(): void
{
echo "ConcreteClass2 says: Overridden Hook1\n";
}
}
function clientCode(AbstractClass $class)
{
$class->templateMethod();
}
echo "Same client code can work with different subclasses:\n";
clientCode(new ConcreteClass1());
echo "Same client code can work with different subclasses:\n";
clientCode(new ConcreteClass2());
复制代码
output:
Same client code can work with different subclasses:
AbstractClass says: I am doing the bulk of the work
ConcreteClass1 says: Implemented Operation1
AbstractClass says: But I let subclasses override some operations
ConcreteClass1 says: Implemented Operation2
AbstractClass says: But I am doing the bulk of the work anyway
Same client code can work with different subclasses:
AbstractClass says: I am doing the bulk of the work
ConcreteClass2 says: Implemented Operation1
AbstractClass says: But I let subclasses override some operations
ConcreteClass2 says: Overridden Hook1
ConcreteClass2 says: Implemented Operation2
AbstractClass says: But I am doing the bulk of the work anyway
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
菜皮日记
关注
全干程序员 2018-08-08 加入
还未添加个人简介
评论