I was wondering if this piece of code does everything the pure factory method design pattern asks for? This is in preparation for my final programming module and I just need to make sure that is is the correct application of the design pattern.
#include <QCoreApplication>
#include <QString>
#include <QDebug>
class Bread
{
public:
virtual void print() = 0;
};
class WhiteBread: public Bread
{
public:
void print() {
qDebug() << "White bread";
}
};
class BrownBread: public Bread
{
public:
void print() {
qDebug() << "Brown bread";
}
};
class BreadFactory {
public:
Bread* makeBread(QString type) {
if (type == "White bread")
return new WhiteBread();
else if (type == "Brown bread")
return new BrownBread();
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
BreadFactory *breadFactory = new BreadFactory();
Bread *breadType;
breadType= breadFactory->makeBread("White bread");
breadType->print();
breadType = breadFactory->makeBread("Brown bread");
breadType->print();
return a.exec();
}
Aucun commentaire:
Enregistrer un commentaire