-
-
Save hcyang1012/6a8f404556ce24b819cb5778cdcf0fcd to your computer and use it in GitHub Desktop.
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 CPU { | |
public: | |
void Work(); | |
private: | |
}; | |
class Intel : public CPU{ | |
public : | |
void WorkIntel(); | |
private: | |
}; | |
class AMD : public CPU{ | |
public: | |
void AMDWork(); | |
private: | |
} | |
enum CPUType{ | |
Intel = 0, | |
AMD = 1 | |
} | |
class CPUFactory{ | |
public: | |
static CPU* Get(const CPUType& type){ | |
if(type == CPUType::Intel){ | |
return new Intel(); | |
}else if (type == CPUType::AMD){ | |
return new AMD(); | |
}else{ | |
return nullptr; | |
} | |
} | |
private: | |
} | |
int main(){ | |
// Downcast!!! | |
Intel *intel_cpu = dynamic_cast<Intel *>(CPUFactory::Get(CPUType::Intel)); | |
AMD *amd_cpu = dynamic_cast<AMD*>(CPUFactory::Get(CPUType::AMD)); | |
intel_cpu->IntelWork(); | |
amd_cpu->AmdWork(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment