模板模式

#if 1

/*
Template Method模式一般应用在具有以下条件
的应用中:
- 具有统一的操作步骤或操作过程
- 具有不同的操作细节
- 存在多个具有同样操作步骤的应用场景,但某些具体的操作细节却各不相同
总结:
在抽象类中统一操作步骤,并规定好接口;让子类实现接口。这样可以把各个具体的子类和操作步骤接耦合
*/
#include <iostream>
using namespace std;

class MakeCar
{
public:
    virtual ~MakeCar(){}
    virtual void makeHead() = 0;
    virtual void makeTail() = 0;
    virtual void makeBody() = 0;
    void make()//模板函数 把业务逻辑做好,子类实现各个逻辑
    {
        makeBody();
        makeHead();
        makeTail();
    }
};


class WBM :public MakeCar
{
public:
    void makeHead()
    {
        cout << "制作WBM车头" << endl;
    }
    void makeTail()
    {
        cout << "制作WBM车尾" << endl;
    }
    void makeBody()
    {
        cout << "制作WBM车身" << endl;
    }   
};

class Bus : public MakeCar
{
public:
    void makeHead()
    {
        cout << "制作Bus车头" << endl;
    }
    void makeTail()
    {
        cout << "制作Bus车尾" << endl;
    }
    void makeBody()
    {
        cout << "制作Bus车身" << endl;
    }
};
int main()
{
    MakeCar* pF = nullptr;

    pF = new WBM;
    pF->make();
    delete pF;

    pF = new Bus;
    pF->make();
    delete pF;

    return 0;
}

#endif

这里写图片描述

#include<iostream>
#include <vector>
#include <string>
using namespace std;

class AbstractClass
{
public:
    virtual ~AbstractClass(){}
    void Show()
    {
        cout << "我是" << GetName() << endl;
    }
protected:
    virtual string GetName() = 0;
};

class Naruto : public AbstractClass
{
protected:
    virtual string GetName()
    {
        return "火影史上最帅的六代目---一鸣惊人naruto";
    }
};

class OnePice : public AbstractClass
{
protected:
    virtual string GetName()
    {
        return "我是无恶不做的大海贼---路飞";
    }
};

class Ye:public AbstractClass
{
public:
    virtual string GetName()
    {
        return "我是美丽的小叶子";
    }
};
//客户端
int main()
{
    Naruto* man = new Naruto();
    man->Show();
    delete man;

    OnePice* man2 = new OnePice();
    man2->Show();
    delete man2;

    Ye* ye = new Ye();
    ye->Show();
    delete ye;
    return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值