【描述】模板设计模式将常用的方法进行封装,创建了一个实施一组方法和功能的抽象的对象。子类通常将这个对象作为模板用于设计。
【UML图】
图1 UML图
1 DrawTemplate有三个抽象的方法:draw() - (protected)、getMethod() - (public)、setMethod() - (protected、纯虚函数-接口)
2 Draw1和Draw2继承了DrawTemplate,Draw1对draw()方法进行了重载、重用了DrawTemplate类的getMethod()方法,实现了setMethod接口。Draw2对draw()、getMethod()方法进行了重载,实现了setMethod接口。
3 对方法的重用是模板模式的优点,如Draw1重用了DrawTemplate类的getMethod方法
【示例代码】
drawtemplate.h
[html] view plaincopyprint #ifndef DRAWTEMPLATE_H
#define DRAWTEMPLATE_H
#include
class DrawTemplate
{
public:
DrawTemplate();
protected:
virtual void draw();
virtual void setMethod(QString method) const = 0;
public:
virtual QString getMethod();
};
#endif // DRAWTEMPLATE_H
#ifndef DRAWTEMPLATE_H
#define DRAWTEMPLATE_H
#include
class DrawTemplate
{
public:
DrawTemplate();
protected:
virtual void draw();
virtual void setMethod(QString method) const = 0;
public:
virtual QString getMethod();
};
#endif // DRAWTEMPLATE_H
drawtemplate.cpp
[html] view plaincopyprint #include
#include "drawtemplate.h"
DrawTemplate::DrawTemplate()
{
qDebug()<<"construct DrawTemplate";
}
void DrawTemplate::draw()
{
qDebug()<<"DrawTemplate::draw()";
setMethod(getMethod());
}
QString DrawTemplate::getMethod()
{
QString test = "Method::DrawTemplate";
qDebug()<<"DrawTemplate::getMethod()";
return test;
}
#include
#include "drawtemplate.h"
DrawTemplate::DrawTemplate()
{
qDebug()<<"construct DrawTemplate";
}
void DrawTemplate::draw()
{
qDebug()<<"DrawTemplate::draw()";
setMethod(getMethod());
}
QString DrawTemplate::getMethod()
{
QString test = "Method::DrawTemplate";
qDebug()<<"DrawTemplate::getMethod()";
return test;
}
draw1.h
[html] view plaincopyprint #ifndef DRAW1_H
#define DRAW1_H
#include "drawtemplate.h"
class Draw1 : public DrawTemplate
{
public:
Draw1();
public:
void draw();
void setMethod(QString) const;
};
#endif // DRAW1_H
#ifndef DRAW1_H
#define DRAW1_H
#include "drawtemplate.h"
class Draw1 : public DrawTemplate
{
public:
Draw1();
public:
void draw();
void setMethod(QString) const;
};
#endif // DRAW1_H
draw1.cpp
[html] view plaincopyprint #include
#include "draw1.h"
Draw1::Draw1()
{
qDebug()<<"construct Draw1";
}
void Draw1::draw()
{
qDebug()<<"Draw1::draw()";
setMethod(getMethod());
}
void Draw1::setMethod(QString method) const
{
qDebug()<
#include
#include "draw1.h"
Draw1::Draw1()
{
qDebug()<<"construct Draw1";
}
void Draw1::draw()
{
qDebug()<<"Draw1::draw()";
setMethod(getMethod());
}
void Draw1::setMethod(QString method) const
{
qDebug()<
draw2.h
[html] view plaincopyprint #ifndef DRAW2_H
#define DRAW2_H
#include "drawtemplate.h"
class Draw2 : public DrawTemplate
{
public:
Draw2();
public:
void draw();
QString getMethod();
void setMethod(QString method) const;
};
#endif // DRAW2_H
#ifndef DRAW2_H
#define DRAW2_H
#include "drawtemplate.h"
class Draw2 : public DrawTemplate
{
public:
Draw2();
public:
void draw();
QString getMethod();
void setMethod(QString method) const;
};
#endif // DRAW2_H
draw2.cpp
[html] view plaincopyprint #include
#include "draw2.h"
Draw2::Draw2()
{
qDebug()<<"construct Draw2";
}
void Draw2::draw()
{
qDebug()<<"Draw2::draw()";
setMethod(getMethod());
}
QString Draw2::getMethod()
{
QString test = "Method::Draw2";
qDebug()<<"Draw2::getMethod()";
return test;
}
void Draw2::setMethod(QString method) const
{
qDebug()<
#include
#include "draw2.h"
Draw2::Draw2()
{
qD