设计模式——模板方法模式

模板方法模式

模板方法模式是一种行为设计模式,在超类中定义一个算法的框架,允许子类在不修改结构的情况下重写算法的特定步骤。

继承的实现应该让重复代码尽可能上升到父类去,而不是每个子类都去重复实现
当我们要完成在某一细节层次一致的一个过程或者一系列步骤时,但是其个别步骤在更详细的层次上的实现可能不同时,我们通常考虑用模板方法来处理。

在这里插入图片描述

#include <iostream>
#include <string>

// 模板方法模式
using std::cout;

// 抽象类定义了一个模板方法,其中包含一些算法的骨架
// 在具体的子类去实现这些细节

class AbstrateClass
{
public:
    void TemplateMethod() const
    {
        this->BaseOperation1();
        this->RequireOperation1();
        this->BaseOperation2();
        this->Hook1();
        this->RequireOperation2();
        this->BaseOperation3();
        this->Hook2();
    }

    // 已经实现的操作
protected:
    void BaseOperation1() const
    {
        cout << "AbstrateClass says: I am doing the bulk of the work\n";
    }

    void BaseOperation2() const
    {
        cout << "AbstrateClass says: But I let subclasses override some operations\n";
    }
    void BaseOperation3() const
    {
        cout << "AbstrateClass says: But I am doing the bulk of the work anyway\n";
    }

    // 子类必须实现的操作
    virtual void RequireOperation1() const = 0;
    virtual void RequireOperation2() const = 0;

    // 俗称钩子 hooks,子类可以实现也可以不实现, 这些钩子在程序中提供了大量的扩展点
    virtual void Hook1() const {}
    virtual void Hook2() const {}
};

// 具体类中必须实现基类的所有抽象操作(纯虚函数),可以选择性地使用默认实现覆盖某些操作
class ConcreteClass1 : public AbstrateClass
{
protected:
    void RequireOperation1() const override
    {
        cout << "ConcreteClass1 says: Implemented Operation1\n";
    }
    void RequireOperation2() const override
    {
        cout << "ConcreteClass1 says: Implememted Operation2\n ";
    }
};

// 通常具体类只覆盖基类的一小部分操作
class ConcreteClass2 : public AbstrateClass
{
protected:
    void RequireOperation1() const override
    {
        cout << "ConcreteClass2 says: Implemented Operation1\n";
    }
    void RequireOperation2() const override
    {
        cout << "ConcreteClass2 says: Implememted Operation2\n ";
    }
    void Hook1() const override
    {
        cout << "ConcreteClass2 says: Overridden Hook1\n";
    }
};

// 客户端代码调用模板方法,不必知道它所操作对象的具体类,只要通过基类提供的接口与对象交互即可
void ClientCode(AbstrateClass *class_)
{
    class_->TemplateMethod();
}

int main()
{
    std::cout << "Same client code can work with different subclasses:\n";
    ConcreteClass1 *concreteClass1 = new ConcreteClass1;
    ClientCode(concreteClass1);
    std::cout << "\n";
    std::cout << "Same client code can work with different subclasses:\n";
    ConcreteClass2 *concreteClass2 = new ConcreteClass2;
    ClientCode(concreteClass2);
    delete concreteClass1;
    delete concreteClass2;
    return 0;
}



`
Same client code can work with different subclasses:
AbstrateClass says: I am doing the bulk of the work
ConcreteClass1 says: Implemented Operation1
AbstrateClass says: But I let subclasses override some operations
ConcreteClass1 says: Implememted Operation2
AbstrateClass says: But I am doing the bulk of the work anyway

Same client code can work with different subclasses:
AbstrateClass says: I am doing the bulk of the work
ConcreteClass2 says: Implemented Operation1
AbstrateClass says: But I let subclasses override some operations
ConcreteClass2 says: Overridden Hook1
ConcreteClass2 says: Implememted Operation2
AbstrateClass says: But I am doing the bulk of the work anyway
`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值