Created
October 4, 2014 21:17
-
-
Save kazmura11/36d05289945b1ed7a184 to your computer and use it in GitHub Desktop.
Design Pattern Prototype
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
#include <iostream> | |
#include <map> | |
#include <string> | |
namespace { | |
enum RECORD_TYPE_en | |
{ | |
CAR, BIKE, PERSON | |
}; | |
const int NUM_OF_RECORD_TYPE_en = 3; | |
} | |
/** | |
* Record is the base Prototype | |
*/ | |
class Record | |
{ | |
public: | |
Record() { } | |
virtual ~Record() { } | |
virtual Record *clone() = 0; | |
virtual void print() = 0; | |
}; | |
/** | |
* CarRecord is a Concrete Prototype | |
*/ | |
class CarRecord : public Record | |
{ | |
private: | |
std::string carName_; | |
int id_; | |
public: | |
CarRecord(std::string carName, int id) | |
: Record(), | |
carName_(carName), | |
id_(id) | |
{ | |
} | |
CarRecord(const CarRecord& rhs) | |
: Record(rhs) // Call the base default copy Constructor | |
{ | |
carName_ = rhs.carName_; | |
id_ = rhs.id_; | |
} | |
~CarRecord() { } | |
Record *clone() override | |
{ | |
return new CarRecord(*this); | |
} | |
void print() override | |
{ | |
std::cout << "Car Record\n" | |
<< "Name : " << carName_ << "\n" | |
<< "Number : " << id_ << "\n\n"; | |
} | |
}; | |
/** | |
* BikeRecord is a Concrete Prototype | |
*/ | |
class BikeRecord : public Record | |
{ | |
private: | |
std::string bikeName_; | |
int id_; | |
public: | |
BikeRecord(std::string bikeName, int id) | |
: Record(), | |
bikeName_(bikeName), | |
id_(id) | |
{ | |
} | |
BikeRecord(const BikeRecord& rhs) | |
: Record(rhs) // Call the base default copy Constructor | |
{ | |
bikeName_ = rhs.bikeName_; | |
id_ = rhs.id_; | |
} | |
~BikeRecord() { } | |
Record *clone() override | |
{ | |
return new BikeRecord(*this); | |
} | |
void print() override | |
{ | |
std::cout << "Bike Record\n" | |
<< "Name : " << bikeName_ << "\n" | |
<< "Number : " << id_ << "\n\n"; | |
} | |
}; | |
/** | |
* PersonRecord is a Concrete Prototype | |
*/ | |
class PersonRecord : public Record | |
{ | |
private: | |
std::string personName_; | |
int age_; | |
public: | |
PersonRecord(std::string personName, int age) | |
: Record(), | |
personName_(personName), | |
age_(age) | |
{ | |
} | |
PersonRecord(const PersonRecord& rhs) | |
: Record(rhs) // Call the base default copy Constructor | |
{ | |
personName_ = rhs.personName_; | |
age_ = rhs.age_; | |
} | |
~PersonRecord() { } | |
Record *clone() override | |
{ | |
return new PersonRecord(*this); | |
} | |
void print() override | |
{ | |
std::cout << "Person Record\n" | |
<< "Name : " << personName_ << "\n" | |
<< "Age : " << age_ << "\n\n"; | |
} | |
}; | |
/** | |
* RecordFactory is the client | |
*/ | |
class RecordFactory | |
{ | |
private: | |
std::map<RECORD_TYPE_en, Record *> recordReference_; | |
public: | |
RecordFactory() | |
{ | |
recordReference_[CAR] = new CarRecord("Ferrari", 5050); | |
recordReference_[BIKE] = new BikeRecord("Yamaha", 2525); | |
recordReference_[PERSON] = new PersonRecord("Tom", 25); | |
} | |
~RecordFactory() | |
{ | |
delete recordReference_[CAR]; | |
delete recordReference_[BIKE]; | |
delete recordReference_[PERSON]; | |
} | |
Record *createRecord(RECORD_TYPE_en enType) | |
{ | |
return recordReference_[enType]->clone(); | |
} | |
}; | |
int main() | |
{ | |
RecordFactory *recordFactory = new RecordFactory(); | |
Record *record; | |
for (int i = 0; i < NUM_OF_RECORD_TYPE_en; i++) | |
{ | |
record = recordFactory->createRecord(static_cast<RECORD_TYPE_en>(i)); | |
record->print(); | |
delete record; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment