Halo, Habr!
Saya bukan ahli yang sangat terkenal, tetapi saya sangat tertarik dengan proses pemrosesan data, serta menulis kode untuk memproses data ini. Dalam proses penulisan berbagai metode pemrosesan data ini, saya mendapat ide tentang otomatisasi parsial, ahem, metode pemrosesan penulisan.
pengantar
Misalkan kita memiliki seperangkat alat (entitas) untuk memproses beberapa jenis aliran data, atau membangun entitas kompleks lainnya, serta kondisi yang terus berubah untuk urutan komposisi entitas ini untuk mereproduksi algoritma pemrosesan.
Menggunakan kendaraan sebagai contoh
Kami memiliki satu set komponen atom:
class EngineA;
class EngineB;
class WingsA;
class WingsB;
class FrameA;
class FrameB;
class WheelsA;
class WheelsB;
dll.
Jika kita membutuhkan mobil, kita cukup mendeklarasikan kelas Mobil , yang memiliki bodi, roda, mesin yang diinginkan, dll. Begitu pula dengan perahu, kami akan mendeklarasikan kelas Perahu , dan dengan cepat membuat sketsa kumpulan bagian-bagian perahu yang diperlukan.
Jika kita membutuhkan perahu, mobil, bahkan pesawat terbang, kita dapat menggunakan pola pabrik, tetapi apa yang harus dilakukan jika kita membutuhkan mobil, perahu, pesawat terbang, dan kita tidak tahu sebelumnya berapa jumlahnya, kapan, dan dalam urutan apa. .
, Car, Boat, Plane ITransport. 2 , 5 - . , , . -
, !
, ITransport
class ITransport
{
public:
virtual void move() = 0;
};
Car, Boat, Plane.
class Car final : public virtual ITransport
{
public:
Car() = default;
~Car() = default;
void move() override
{
std::cout << "Car is move" << std::endl;
// do something with parts
}
private:
std::unique_ptr < IFrame > _frame;
std::unique_ptr < IWheels > _wheels;
std::unique_ptr < IEngine > _engine;
};
class Boat final : public virtual ITransport
{
public:
Boat() = default;
~Boat() = default;
void move() override
{
std::cout << "Boat is move" << std::endl;
// do something with parts
}
private:
std::unique_ptr < IFrame > _frame;
std::unique_ptr < IEngine> _engine;
};
class Plane final : public virtual ITransport
{
public:
Plane() = default;
~Plane() = default;
void move() override
{
std::cout << "Plane is move" << std::endl;
// do something with parts
}
private:
std::unique_ptr < IFrame > _frame;
std::unique_ptr < IEngine> _engine;
std::unique_ptr < IWings > _wings;
};
2 , , 3 , .
, . .
enum class VehTypes
{
Car,
Boat,
Plane
};
static std::map < std::string, VehTypes > VehCast{
{"Car", VehTypes::Car},
{"Boat", VehTypes::Boat},
{"Plane", VehTypes::Plane}
};
, .
class Conveyor final
{
public:
using _TyParameters = std::map < std::string, std::string >;
using _TyStorage = std::vector < _TyParameters >;
Conveyor(const _TyStorage& blueprints)
: _blueprints(blueprints) { }
~Conveyor() = default;
std::vector < Vehicles::ITransport* > vehicles()
{
std::vector < Vehicles::ITransport* > result;
for (auto&& blueprint : _blueprints)
{
switch (VehCast[blueprint["type"]])
{
case VehTypes::Car: result.emplace_back(new Vehicles::Car());
break;
case VehTypes::Boat: result.emplace_back(new Vehicles::Boat());
break;
case VehTypes::Plane: result.emplace_back(new Vehicles::Plane());
break;
}
}
return result;
}
private:
_TyStorage _blueprints;
};
, .
:
-
- , .
using _TyParameters = std::map < std::string, std::string >;
using _TyStorage = std::vector < _TyParameters >;
, ( ).
, - , .
( ) .
.
.
Conveyor::_TyStorage blueprints
{
{
{"type", "Car"}, {"engineType", "EngineA"}, {"wheelsType", "WheelsB"}, etc..
},
{
{"type", "Car"},
},
{
{"type", "Boat"},
},
{
{"type", "Plane"},
},
{
{"type", "Plane"},
},
{
{"type", "Plane"}
},
{
{"type", "Boat"}
},
};
Conveyor conveyor(blueprints);
for (auto&& transport : conveyor.vehicles())
{
transport->move();
}
, , .
, , / .
Dari titik lemah pendekatan ini untuk membuat objek, saya dapat mencatat kebutuhan untuk menambahkan tipe baru ke kamus setiap saat, serta terus menambahkan penafsir parameter teks baru jika Anda terus menambahkan komponen atom baru.