Skip to content

Instantly share code, notes, and snippets.

@mgarg1
Last active October 10, 2016 21:58
Show Gist options
  • Save mgarg1/c51dce98a48a0b00e0946561bbd81e3c to your computer and use it in GitHub Desktop.
Save mgarg1/c51dce98a48a0b00e0946561bbd81e3c to your computer and use it in GitHub Desktop.
Abstract Factory Design Pattern GOF Book example
class WidgetFactory{
public:
virtual ScrollBar *createScrollBar() = 0;
virtual Window *createWindow() = 0;
};
class Window{
public:
virtual void Paint() = 0;
};
class ScrollBar{
public:
virtual void Paint() = 0;
};
class MotifScrollBar() : public ScrollBar{
public:
void paint(){
std::cout << "MotifScrollBar";
}
};
class PMScrollBar() : public ScrollBar{
public:
void paint(){
std::cout << "PMScrollBar";
}
};
class MotifWindow() : public Window{
public:
void paint(){
std::cout << "MotifWindow";
}
};
class PMWindow() : public Window{
public:
void paint(){
std::cout << "PMWindow";
}
};
class MotifWidgetFactory : public WidgetFactory{
public:
ScrollBar *createScrollBar(){
return new MotifScrollBar();
}
Window *createWindow(){
return new MotifWindow();
}
};
class PMWidgetFactory : public WidgetFactory{
public:
ScrollBar *createScrollBar(){
return new PMScrollBar();
}
Window *createWindow(){
return new PMWindow();
}
};
enum appearanceStds{ PM, Motif};
void main(){
// var appearance = Settings.Appearance;
WidgetFactory *factory;
appearanceStds appearanceChoice = PM;
switch (appearanceChoice)
{
case PM:
factory = new PMWidgetFactory();
break;
case Motif:
factory = new MotifWidgetFactory();
break;
default:
cout << "This Appearance doesn't exist\n";
}
ScrollBar *s = factory->createScrollBar();
s->paint();
Window *w = factory->createWindow();
w->paint();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment