Skip to content

Instantly share code, notes, and snippets.

@itayB
Created September 27, 2016 19:04
Show Gist options
  • Select an option

  • Save itayB/15323ff812b95dca92cedb87fc49246c to your computer and use it in GitHub Desktop.

Select an option

Save itayB/15323ff812b95dca92cedb87fc49246c to your computer and use it in GitHub Desktop.
Abstract Factory Example (Design Pattern)
#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