Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active May 9, 2020 17:24
Show Gist options
  • Save antic183/dcf82aa0afc93d7ce7a31e4b12e26cab to your computer and use it in GitHub Desktop.
Save antic183/dcf82aa0afc93d7ce7a31e4b12e26cab to your computer and use it in GitHub Desktop.
C++ oop playground. The pattern doesn't have to make sense. Below the extendet factory with singleton per type.
#pragma once
#include <string>
#include "IMotoVehicle.h";
using namespace std;
class Car: public IMotoVehicle {
int doors;
public:
Car(string label, string colour, int doors) : IMotoVehicle(label, colour) {
this->doors = doors;
}
// override
IMotoVehicle* startMotor() {
if (!isMotorOn) {
isMotorOn = true;
cout << "--> motor CAR was started" << endl;
}
return this;
}
// override
IMotoVehicle* stopMotor() {
if (isMotorOn) {
isMotorOn = false;
cout << "--> motor CAR was stopped" << endl;
}
return this;
}
// override
string getData() {
return "Car: label = " + label
+ ", color " + this->color
+ ", motor is on = " + to_string(this->isMotorOn)
+ ", doors = " + to_string(doors)
+ ", uniqueId = " + to_string(uniqueId);
}
};
#include <iostream>
#include "Car.h"
#include "EVehicleType.h"
#include "VehicleFactory.h"
using namespace std;
int main() {
VehicleFactory* factory = nullptr;
IMotoVehicle* instance = nullptr;
try {
factory = new VehicleSingletonFactory(CAR, "Audi", "blue");
factory = new VehicleSingletonFactory(CAR, "BMW", "green"); // will be ignored because the factory only creates one instance per type
instance = factory->getInstance(); // in this case the "Audi, blue" is behind the instance.
cout << instance->startMotor()->getData() << endl;
cout << instance->stopMotor()->getData() << endl;
cout << "------------------------------------------------------------------" << endl << endl << endl;
delete instance;
delete factory;
factory = new VehicleSingletonFactory(MOTOBIKE, "Yamaha", "green");
instance = factory->getInstance();
cout << instance->startMotor()->getData() << endl;
cout << instance->stopMotor()->getData() << endl;
} catch(...) {
cout << "throws an exception...";
}
// prevent memory leak!
delete factory;
factory = nullptr;
delete instance;
instance = nullptr;
return 1;
};
#pragma once
enum EVehicleType {
CAR, MOTOBIKE, UNKNOWN
};
#pragma once
#include <string>
using namespace std;
class IMotoVehicle {
protected:
string label;
string color;
bool isMotorOn;
int uniqueId;
public:
IMotoVehicle(string label, string colour) {
this->label = label;
this->color = colour;
isMotorOn = false;
uniqueId = rand();
}
// below methods are to implement in child class
public:
virtual IMotoVehicle* startMotor() = 0;
virtual IMotoVehicle* stopMotor() = 0;
virtual string getData() = 0;
};
#pragma once
#include <iostream>
#include "IMotoVehicle.h"
using namespace std;
class MotoBike : public IMotoVehicle {
public:
MotoBike(string label, string colour) : IMotoVehicle(label, colour) {
}
// override
IMotoVehicle* startMotor() {
if (!isMotorOn) {
isMotorOn = true;
cout << "--> motor MOTOBIKE was started" << endl;
}
return this;
}
// override
IMotoVehicle* stopMotor() {
if (isMotorOn) {
isMotorOn = false;
cout << "--> motor MOTOBIKE was stopped" << endl;
}
return this;
}
// override
string getData() {
return "MotoBike: label = " + label
+ ", color " + this->color
+ ", motor is on = " + to_string(this->isMotorOn)
+ ", uniqueId = " + to_string(uniqueId);
}
};
#pragma once
#include "EVehicleType.h"
#include "IMotoVehicle.h"
#include "Car.h"
#include "MotoBike.h"
using namespace std;
class VehicleSingletonFactory {
IMotoVehicle* instance = nullptr;
public:
VehicleSingletonFactory(EVehicleType vehicleType, string label, string color) {
switch (vehicleType) {
case CAR:
if(this->instance != nullptr && dynamic_cast<CAR*>(this->instance) != nullptr) {
this->instance = new Car(label, color, 4);
}
break;
case MOTOBIKE:
if(this->instance != nullptr && dynamic_cast<MOTOBIKE*>(this->instance) != nullptr) {
this->instance = new MotoBike(label, color);
}
break;
default:
delete this->instance;
this->instance = nullptr;
throw "The vehicle type isn't correctly. It must be of type CAR or MOTOBIKE";
break;
}
}
IMotoVehicle* getInstance() {
return instance;
}
public:
~VehicleSingletonFactory() {
delete this->instance;
this->instance = nullptr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment