Last active
October 10, 2016 21:58
-
-
Save mgarg1/c51dce98a48a0b00e0946561bbd81e3c to your computer and use it in GitHub Desktop.
Abstract Factory Design Pattern GOF Book example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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