Skip to content

Instantly share code, notes, and snippets.

@XoLinA
Created March 24, 2026 13:29
Show Gist options
  • Select an option

  • Save XoLinA/ee9d3198c8396300215bd3dacabe4cdd to your computer and use it in GitHub Desktop.

Select an option

Save XoLinA/ee9d3198c8396300215bd3dacabe4cdd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class AbstractDevice
{
protected:
string producer;
public:
virtual AbstractDevice* clone() = 0;
virtual void show() = 0;
};
class Engine : public AbstractDevice
{
public:
int power;
void SetPower(int t) { power = t; }
AbstractDevice* clone() override { return new Engine(*this); }
void show() override { cout << "Engine power: " << power << endl; }
};
class Transmission : public AbstractDevice
{
public:
string type;
void SetType(string t) { type = t; }
AbstractDevice* clone() override { return new Transmission(*this); }
void show() override { cout << "Transmission: " << type << endl; }
};
class Manufacturer : public AbstractDevice
{
public:
string name;
Manufacturer() {}
void SetName(string t) { name = t; }
AbstractDevice* clone() override { return new Manufacturer(*this); }
void show() override { cout << "Manufacturer: " << name << endl; }
};
class Year : public AbstractDevice
{
public:
int year;
void SetYear(int t) { year = t; }
AbstractDevice* clone() override { return new Year(*this); }
void show() override { cout << "Year: " << year << endl; }
};
class InteriorColor : public AbstractDevice {
public:
string incolor;
void SetIncolor(string t) { incolor = t; }
AbstractDevice* clone() override { return new InteriorColor(*this); }
void show() override { cout << "Interior color: " << incolor << endl; }
};
class Price : public AbstractDevice
{
public:
int price;
void SetPrice(int t) { price = t; }
AbstractDevice* clone() override { return new Price(*this); }
void show() override { cout << "Price: " << price << "$" << endl; }
};
class Prototype
{
public:
virtual Prototype* Clone() = 0;
};
class Car : public Prototype
{
Engine* engine = nullptr;
Transmission* transmission = nullptr;
Manufacturer* manufacturer = nullptr;
Year* year = nullptr;
Price* price = nullptr;
public:
void SetEngine(Engine* engine)
{
this->engine = engine;
}
void SetTransmission(Transmission* transmission)
{
this->transmission = transmission;
}
void SetManufacturer(Manufacturer* manufacturer)
{
this->manufacturer = manufacturer;
}
void SetYear(Year* year)
{
this->year = year;
}
void SetPrice(Price* price)
{
this->price = price;
}
Prototype* Clone() override
{
Car* au = new Car;
au->engine = this->engine ? (Engine*)this->engine->clone() : nullptr;
au->transmission = this->transmission ? (Transmission*)this->transmission->clone() : nullptr;
au->manufacturer = this->manufacturer ? (Manufacturer*)this->manufacturer->clone() : nullptr;
au->year = this->year ? (Year*)this->year->clone() : nullptr;
au->price = this->price ? (Price*)this->price->clone() : nullptr;
return au;
}
void Show()
{
cout << "\n--- Car ---" << endl;
if (engine) engine->show();
if (transmission) transmission->show();
if (manufacturer) manufacturer->show();
if (year) year->show();
if (price) price->show();
}
};
class AuPrototypes
{
map<string, Car*> auprototypes;
public:
AuPrototypes()
{
InitializeAuPrototypes();
}
Car* operator[](string key)
{
return auprototypes.count(key) ? auprototypes[key] : nullptr;
}
void InitializeAuPrototypes()
{
auprototypes["High-speed"] = CreateSpeedConfig();
auprototypes["Passenger"] = CreatePassengerConfig();
}
Car* CreateSpeedConfig()
{
Car* config = new Car();
Engine* engine = new Engine();
engine->SetPower(300);
Transmission* transmission = new Transmission();
transmission->SetType("Auto");
Manufacturer* manufacturer = new Manufacturer();
manufacturer->SetName("BMW");
Year* year = new Year();
year->SetYear(2022);
Price* price = new Price();
price->SetPrice(50000);
config->SetEngine(engine);
config->SetTransmission(transmission);
config->SetManufacturer(manufacturer);
config->SetYear(year);
config->SetPrice(price);
return config;
}
Car* CreatePassengerConfig()
{
Car* config = new Car();
Engine* engine = new Engine();
engine->SetPower(150);
Transmission* transmission = new Transmission();
transmission->SetType("Manual");
Manufacturer* manufacturer = new Manufacturer();
manufacturer->SetName("Toyota");
Year* year = new Year();
year->SetYear(2018);
Price* price = new Price();
price->SetPrice(20000);
config->SetEngine(engine);
config->SetTransmission(transmission);
config->SetManufacturer(manufacturer);
config->SetYear(year);
config->SetPrice(price);
return config;
}
};
int main()
{
cout << "Enter config prototype name: ";
string prototypeName;
cin >> prototypeName;
AuPrototypes pc;
Car* prototype = pc[prototypeName];
if (prototype != nullptr)
{
Car* clone = (Car*)prototype->Clone();
cout << "\nPrototype:";
prototype->Show();
cout << "\nClone:";
clone->Show();
}
else
{
cout << "Error----------------------";
}
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment