Created
September 27, 2016 19:04
-
-
Save itayB/15323ff812b95dca92cedb87fc49246c to your computer and use it in GitHub Desktop.
Abstract Factory Example (Design Pattern)
This file contains hidden or 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
| #include <iostream> | |
| using namespace std; | |
| class Parts { | |
| string specification; | |
| public: | |
| // Constructor | |
| Parts(string specification) { | |
| this->specification = specification; | |
| } | |
| string getSpecification() { | |
| return specification; | |
| } | |
| }; | |
| class Computer { | |
| public: | |
| virtual Parts getRAM() = 0; | |
| virtual Parts getProcessor() = 0; | |
| virtual Parts getMonitor() = 0; | |
| }; | |
| class PC : public Computer { | |
| public: | |
| Parts getRAM() { | |
| return Parts("512 MB"); | |
| } | |
| Parts getProcessor() { | |
| return Parts("Celeron"); | |
| } | |
| Parts getMonitor() { | |
| return Parts("15 inches"); | |
| } | |
| }; | |
| class Server : public Computer { | |
| public: | |
| Parts getRAM() { | |
| return Parts("4 GB"); | |
| } | |
| Parts getProcessor() { | |
| return Parts("Inter P4"); | |
| } | |
| Parts getMonitor() { | |
| return Parts("17 inches"); | |
| } | |
| }; | |
| class ComputerType { | |
| Computer* comp; | |
| public: | |
| Computer* getComputer(string computerType) { | |
| if (computerType.compare("PC")) | |
| comp = new PC(); | |
| else if(computerType.compare("Server")) | |
| comp = new Server(); | |
| return comp; | |
| } | |
| }; | |
| int main() { | |
| ComputerType type = ComputerType(); | |
| Computer* computer = type.getComputer("Server"); | |
| cout << "Monitor: " << computer->getMonitor().getSpecification() << endl; | |
| cout << "RAM: " << computer->getRAM().getSpecification() << endl; | |
| cout << "Processor: " << computer->getProcessor().getSpecification() << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment