现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

代码版设计模式16:模板方法模式【行为型】

2013-03-05 12:18 工业·编程 ⁄ 共 927字 ⁄ 字号 暂无评论

#include <iostream>
#include <list>
using namespace std;

//
struct IResume
{
public:
    virtual ~IResume() { }

    virtual void SetPersonalInfo() = 0;
    virtual void SetEducationInfo() = 0;
    virtual void SetWorkInfo() = 0;

    void FillResume()
    {
        SetPersonalInfo();
        SetEducationInfo();
        SetWorkInfo();
    }
};

class CResumeA : public IResume
{
public:
    virtual ~CResumeA() { }

    virtual void SetPersonalInfo() { cout<<"A person info."<<endl; }
    virtual void SetEducationInfo() { cout<<"A education info."<<endl; }
    virtual void SetWorkInfo() { cout<<"A work info."<<endl; }
};

class CResumeB : public IResume
{
public:
    virtual ~CResumeB() { }

    virtual void SetPersonalInfo() { cout<<"B person info."<<endl; }
    virtual void SetEducationInfo() { cout<<"B education info."<<endl; }
    virtual void SetWorkInfo() { cout<<"B work info."<<endl; }
};

//
void main()
{
    IResume *pResume = new CResumeA();
    pResume->FillResume();
    delete pResume;

    pResume = new CResumeB();
    pResume->FillResume();
    delete pResume;
}

给我留言

留言无头像?