I'm learning about design pattern and I tried to implement a Factory Method example, based on the GoF book.
Can I say that this is a correct implementation of it ? If not I will be glad to find out what are the pitfalls.
#include <iostream>
#include <vector>
using namespace std;
class Widget {
public:
virtual void draw() = 0;
};
class Win7Widget : public Widget {
public:
void draw() { cout << "Win 7 widget" << endl; }
};
class Win8Widget : public Widget {
public:
void draw() { cout << "Win 8 widget" << endl; }
};
class Factory {
public:
virtual Widget* Create() = 0;
virtual ~Factory() {}
};
class Win7Factory: public Factory {
public:
Widget* Create() { return new Win7Widget; }
};
class Win8Factory: public Factory {
public:
Widget* Create() { return new Win8Widget; }
};
int main()
{
unique_ptr<Factory> win7fact(new Win7Factory);
unique_ptr<Factory> win8fact(new Win8Factory);
vector<Widget*> widgets;
widgets.push_back(win7fact->Create());
widgets.push_back(win8fact->Create());
for(const auto& f : widgets)
{
f->draw();
}
return 0;
}