IGiveGift.h
#ifndef IGIVEGIFT_H
#define IGIVEGIFT_H
class IGiveGift
{
public:
IGiveGift() {}
virtual void giveDolls() = 0;
};
#endif // IGIVEGIFT_H
pursuit.h
#ifndef PURSUIT_H
#define PURSUIT_H
#include <QString>
#include "IGiveGift.h"
class Pursuit : public IGiveGift
{
QString m_name;
public:
Pursuit(QString name)
{
this->m_name = name;
}
void giveDolls()
{
qDebug() << m_name << " 送你洋娃娃";
}
};
#endif // PURSUIT_H
proxy.h
#ifndef PROXY_H
#define PROXY_H
#include <QString>
#include "IGiveGift.h"
#include "pursuit.h"
class Proxy : public IGiveGift
{
public:
Proxy(QString name)
{
m_gg = new Pursuit(name);
}
void giveDolls()
{
m_gg->giveDolls();
}
private:
Pursuit *m_gg;
};
#endif // PROXY_H
main
#include <QApplication>
#include <QtDebug>
#include "proxy.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Proxy *proxy = new Proxy("小明");
proxy->giveDolls();
return a.exec();
}
真是對象(Pursuit)、代理對象(proxy)同時繼承一個接口(IGiveGift)。真實對象實現接口方法。代理對象中定義真實對象,接口方法中調用真實對象的方法。從而實現對真實對象方法的封裝
UML