关于后端:行为型设计模式模板方法-Template-Method

31次阅读

共计 2340 个字符,预计需要花费 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

本文由 mdnice 多平台公布

正文完
 0