Created
December 30, 2017 11:47
-
-
Save FONQRI/ecbdb4856f6cd9a2f722daec32b17392 to your computer and use it in GitHub Desktop.
Abstract Factory
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> | |
#include <string> | |
using namespace std; | |
// Abstract base class | |
class Mobile { | |
public: | |
virtual ~Mobile(); | |
virtual string Camera() = 0; | |
virtual string KeyBoard() = 0; | |
void PrintSpecs() | |
{ | |
cout << Camera() << endl; | |
cout << KeyBoard() << endl; | |
} | |
}; | |
// Concrete classes | |
class LowEndMobile : public Mobile { | |
public: | |
~LowEndMobile(); | |
string Camera() { return "2 MegaPixel"; } | |
string KeyBoard() { return "ITU-T"; } | |
}; | |
LowEndMobile::~LowEndMobile() {} | |
// Concrete classes | |
class HighEndMobile : public Mobile { | |
public: | |
~HighEndMobile(); | |
string Camera() { return "5 MegaPixel"; } | |
string KeyBoard() { return "Qwerty"; } | |
}; | |
HighEndMobile::~HighEndMobile() {} | |
// Abstract Factory returning a mobile | |
class MobileFactory { | |
public: | |
Mobile *GetMobile(string type); | |
}; | |
Mobile *MobileFactory::GetMobile(string type) | |
{ | |
if (type == "Low-End") | |
return new LowEndMobile(); | |
if (type == "High-End") | |
return new HighEndMobile(); | |
return NULL; | |
} | |
int main() | |
{ | |
MobileFactory *myFactory = new MobileFactory(); | |
Mobile *myMobile1 = myFactory->GetMobile("Low-End"); | |
myMobile1->PrintSpecs(); | |
Mobile *myMobile2 = myFactory->GetMobile("High-End"); | |
myMobile2->PrintSpecs(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment